Selenium webdriver unable to find this one particular element - java

I tried looking around for a solution, but haven't found any. Webdriver is unable to find this one particular element for some reason.
Here's the html code:
<div id=​"cboxOverlay" style="opacity: 1; cursor: auto; display: none;">​</div>​
And I am using xpath to find this element
By.xpath("//div[#id = 'cboxOverlay' and contains(#style, 'display: none;')]")
When I use firefinder, I can find this element. But it times out and doesnt find the element when I run the code
WebElement cboxOverlayWebElement = driver.findElement(cboxOverlay);
I have also tried using explicit wait to wait till the element is clickable or visible. Also I am using Java.
Could someone help me? Thanks!!

#Sweta
In your html code there is "display: none" means cboxoverlay would not be display. So webdriver also did not get it. Try to fine out what action makes this overlay display and then according write your code.

Related

Unable to locate an element even when the xpath is correct

i'm tried to select an element from an auto suggestion field but i got always an error saying that the element could not be found even that i'm sure my xpath is correct
here's my code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")));
driver.findElement(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")).click();
it should find //*#class=\"ui-menu-item-with-icon ui-menu-item\" which is the first suggestion albert cammus
here's the outerHtml
<li class="ui-menu-item-with-icon ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">
<span class="item-icon"></span>
Albert Camus (SARCELLES)</a>
</li>"
Your XPath is more or less OK apart from using wildcard which may result into longer processing so you can go for li instead of *.
Another option is sticking to the <a> tag containing the text you would like to click using normalize-space() function something like:
//a[normalize-space()="Albert Camus (SARCELLES)"]
Also your popup may reside within an iframe so you might have to switch the webdriver context to the relevant iframe element.
Why don't you try linkText over Xpath ?
linkText is more stable then Xpath, there's no doubt about that.
Code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Albert Camus (SARCELLES)")));
I'm not very sure about spaces in your HTML, that's the reason why I have used partialLinkText

Locate an element created by javascript using selenium webdriver

I'm new in Selenium and Java, i'm work in a Automation testing project using selenium (java) , today i have a problem with driver.findElement, the element i want to find was created by javascript, i'm sure about using WebDriverWait to wait to target element to present, even i use thread.sleep and wait for it for about few minutes but webdriver still can not locate that element.
This is my problem, in html/js code i have a button, click this button javascript will create a div and i need use selenium to locate it to make Automatio Testing work.
After click a button, javascript makes a div look like :
<div id="zm-view-frame" class="fade in"><div class="zm-view-header"><div class="zm-view-back"><a onclick="WFLandingpage.closePreview();" id="zm-view-close" class="button" href="javascript:void(0);"><span>Close</span></a></div><div id="zm-view-button"><span>Desktop</span><span>Tablet</span><span>Mobile</span><span>Rotate</span></div></div><iframe id="zm-display-iframe" src="page=mvc_landingpages&act=templatepage&preview=1&templateId=landingpage3" style="padding-top: 63px; margin: 0px auto;" scrolling="yes" width="100%" height="609"></iframe></div>
then, in java code i wrote :
this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("zm-view-frame")));
Then :
Tester.Browser.findElement(By.id("zm-view-frame")).click();
of course, i have defined this.wait :
public WebDriverWait wait = new WebDriverWait(Tester.Browser, 100);
and i get a wait timeout exception
I even use many type of By such as Xpath, css selector, class ... but still no luck.
I also use Selenium IDE for firefox to get element, export it to java code and view in my editor then do the same but it's not work for me again.
i'm really stuck on this, can you give me some help ?
and sorry for my bad english, many thanks !
UPDATE - this is my html structure :
html structure
UPDATE - I found the bug and have a solution
Reason : Before above code i have few line of codes to check if a iframe available, the code like :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).
I found the bug and have a solution
Reason : Before above code i have few lines of code to check if a iframe available, the code like below :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).

#FindAll Is Kind of Slow When Trying to Locate Multiple Elements

I have a page that is localized and the "Create Account" WebElement can be English, Chinese or Japanese. I am using Selenium, Java and TestNG framework to run a test to click on this element. However, the slow performance when using this #FindAll to identify the page makes me wonder if there are any better way to do this.
The element from Inspect element while "English" locale is selected:
<div class="form-group">
<a translate="create-account" class="pointer ng-scope" ng-click="vm.createAccount()">Create Account</a>
</div>
My FindAll declaration:
#FindAll({
#FindBy(linkText="Create Account"),
#FindBy(linkText="创建账号"),
#FindBy(linkText="アカウントを作成")
})
private List<WebElement> createAccount;
As a baseline to compare, if I use the #FindAll above, it takes about 15 seconds before Webdriver clicks on the link. If I use just #FindBy, it takes about 2-3 seconds. However, #FindBy does not work for me as I need to be able to locate the correct locale to click on the link.
You could use a single css selector like:
a[ng-click*='createAccount']
Or one of the xpaths:
//a[contains(#ng-click, 'createAccount')]
//a[contains(text(), 'Create Account') or contains(text(), '创建账号') or contains(text(), 'アカウントを作成')]
For css if you pass part of the attribute value then it should be [#attributeName*='part_of_attribute_value']
Please take a look here to view a basic list of css rules w3schools css selectors
Thanks #lauda for helping out and the link to w3 css selectors.
I actually found two more ways that I can identify this link using css:
#FindBy(css="a[translate='create-account']")
private WebElement CreateAccount;
and
#FindBy (css="a.pointer.ng-scope")
private WebElement CreateAccount;
However, not sure why the original solution that lauda posted did not work for me though.
a[ng-click*='createAccount']

Selenium sees textbox as hidden even if I can see it in the browser

I have a password textbox that is something like this
<input class="blahblah" id="someId" type="password"></input>
I am able to see this textbox in the browser and am able to manually insert password.
However when I test this ui using selenium, although it finds the element correctly, but when it tries to click the element, it throws an error
"org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with"
I did a check in code using
Boolean isDisplayed=el.isDisplayed();//false
Boolean isEnabled=el.isEnabled();//true
The isDisplayed came up false and isEnabled came up true. There is a 15 second delay added to give the page enough time to load (the page loads instantly). So adding a delay will not fix the problem.
I verified using firefox developer tools that the id it was finding was of the correct element.
Why does selenium think its invisible even if I am able to see it in the browser? Could it be that one of the parent elements has some style attribute that selenium doesn't like? Or is it a bug in the selenium driver?
I am using selenium driver for java version 2.45.0
The problem is that the desired input is really invisible due to the display: none being set on it's parent table:
<table title="Type a password."
class="dxeTextBoxSys dxeTextBox_MyCompany "
id="ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB"
style="width: 100%; border-collapse: collapse; display: none;"
border="0" cellpadding="0" cellspacing="0">
Most likely, the table is becoming visible on a particular user action that you need to determine.
But, alternatively, you can make the table visible through javascript:
WebElement table = driver.findElement(By.id("ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].style.display = 'block';", table);
In case the above doesn't make any difference.
There is an another hidden password input that can be important:
<input value=""
name="ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS"
type="hidden">
You can try making it visible and sending keys to it:
WebElement password = driver.findElement(By.name("ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('type', 'text');", password);
password.sendKeys("MyPassword");
In case the above doesn't work.
You can set the input value through javascript:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB_I').setAttribute('value', 'MyPassword');");
Possibly, Selenium is going too fast through your DOM. Has happened with me several times and your element hasn't fully loaded into DOM.
I am more familiar with the PHP/PHPUnit libraries available for Selenium, but perhaps you can introduce a temporary wait with a command similar to waitForElementPresent.
Also, if you have control of the code, can you give a 'name' attribute to your input field as well? It could not hurt anything to do so.
Have a look at the DOM elements and verify that there is no parent element with a display: none etc, when i encountered an issue like this that was the problem.
Are you able to get information from the element by XPath? This was my work around.
I have faced this kind of issue many times. the first thing comes into my mind is probably the selector you are using is not unique or not returning THE element you are looking for. Since,
Boolean isDisplayed=el.isDisplayed();//false
Boolean isEnabled=el.isEnabled();//true
does not return NoSuchElement exception I do not think it's a element load issue. A quick check can tell you what's going on
driver.findElements(By.cssSelector("the css")).size(); and see how many count it returns.

Selenium Unable to Find Element

I am trying to find an element on a website using Selenium. The page I am looking at is:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
Using an explicit wait worked for me (tried both chrome and firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into:

Categories