I'm using Chromedriver to try and find the following element:
<td class="section-menuoption"
onmouseover="this.className='section-menuoption-hilite';"
onmouseout="this.className='section-menuoption';" align="left"
onclick="self.jssDetails.location='products.php?xUser=kery&xRand=123';">
Scheduled Changes
</td>
When using the SeleniumIDE in Firefox I can use this selector without issue:
//*[contains(text(), 'Scheduled Changes')]
However, when I attempt to use this selector with the Chromedriver I get a "no such element" error (running via maven). My Chromedriver code looks likes this:
WebElement box = driver.findElement(By.xpath("//*[contains(text(), 'Scheduled Changes')]"));
System.out.print(box.getText());
box.click();
I've tried different quoting strategies, different xpaths (that also resolve correctly in the SeleniumIDE), but with no success. The only thing I can think of now is changing my XPath implementation, but I'm not even sure that's possible with Chromedriver.
Any help would be very appreciated.
First, make sure it it not in any kind of frames. Otherwise you need switch into the frame first, like this.
Then try use css selector instead of XPath. For example (you may need try different variations):
WebElement box = driver.findElement(By.cssSelector("td[onclick*=\"self.jssDetails.location='products.php?xUser=kery&xRand=123';\"]"));
Also, you might want to post every XPaths you have tried, to help us analyse.
Related
I ran into a little issue today while validating some input fields in our application.
Looks like we can't call the selenium java method clear() to clear the input and I know I can iterate over the string and use sendKeys(Keys.Backspace) but clear method should do the work?
Anyone else ran into the same issue and how did you work around this problem.?
Our app is built upon Ant libraries
Selenium
String hotmarketNameEdit = "ant-input";
WebElement edit = driver.findElement(By.className(hotmarketNameEdit));
edit.click();
edit.clear();
HTML
<input type="text" id="label" class="ant-input ant-input-sm" value="AUTOMATION TEST - Thu Oct 21 09:54:15 MDT 2021" xpath="1">
Thanks for any inputs
I've personally come across this issue quite a few times, that Selenium .clear() method does not work properly.
Selenium community is aware of this issue, please see the bug reported here - ClearText does not work
You can go through the above-mentioned link.
Workaround :
Majority of time, I try to simulate Ctrl + A to select the text and just press delete after that.
If You are using Java as a binding language, You can do the following
webElement.sendKeys(Keys.CONTROL + "a")
webElement.sendKeys(Keys.DELETE)
have you tried JavaScriptExecutor?
JavascriptExecutor jse = (JavascriptExecutor) indexWebDriver;
jse.executeScript("arguments[0].value=''",edit);
check this one also
How can I consistently remove the default text from an input element with Selenium?
Can you please try this work around, passing both selecting and deleting keywords in one command
driver.findElement(By.name("username")).sendKeys(Keys.chord(Keys.CONTROL,"a", Keys.DELETE));
I am trying to click on a checkbox that only has a name.
Here is the HTML:
<td class="checkbox-column"><input type="checkbox" name="link-active[138]"></td>
Here is my latest attempt to find the element, I have tried a lot of different methods. I also need to grab it without the "[127]" part since that is dynamic in this case.
driver.findElement(By.xpath("//*[text()[contains(.,'link-active')]]")).click();
You are using wrong xpath, correct xpath is:
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
You need to check the name attribute.
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
xpath has a name attribute method, you can try this
driver.findElement(By.xpath("//*[contains(name(),'link-active')]")).click();
You can simply introduce webdriverwait to let your script knows that it is visible and enabled and then you can try to click. Like this :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#name,'link-active')]"))).click();
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 :).
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.
I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.
Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.
I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']
We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.