I would like to clik one button, as of now I'm using a XPATH, but sometime the page is changed and I've to update the XPATH.
I'm checking a different way (search and click a image like play.png, click by text, etc.), in order to obtain same result.
Following the portion of the HTML code:
<button class="btn btn-green false">PLAY</button>
EDIT:
I've tried with the code
driver.findElement(By.xpath("//*[contains(text(),’PLAY’]")).click();
but it goes in exception
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[contains(text(),’PLAY’] (tried for 10 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at seleniumTest.TestSelenium.play(TestSelenium.java:300)
at seleniumTest.TestSelenium.open_url(TestSelenium.java:277)
at seleniumTest.TestSelenium.seleniumTest(TestSelenium.java:108)
at seleniumTest.TestSelenium.main(TestSelenium.java:85)
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(),’PLAY’] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(),’PLAY’]' is not a valid XPath expression.
You can also use contains or starts-with() when the values of the attributes are dynamic i.e. changing like:
//*[contains(text(),’PLAY’)]
You have to find something that doesn't change.
For example you could do that in XPath using wildcard:
//button[text()='PLAY']
Also you could use the other ways to identify elements in Selenium.
For example By.id(...) if your elements have a unique id.
See Selenium doc for class By
Related
Consider the following <select> from an internal Selenium test page:
<select id="invisi_select" style="opacity:0;">
<option selected value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
It is used to simulate an invisible element as the id suggests, which is done by setting opacity to 0.
Although the element is not visible, a user can actually interact with it. If I open the page in a browser and click on the element's position, the select menu opens. I believe this is also why WebElement#isDisplayed() returns true for this element, which is also what these old Selenium issues suggest:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1610
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1941
To execute actions such as clicks, we recently switched to the Java interactions API for several reasons, e.g., to prevent ElementClickInterceptedExceptions. (Please note that this is not about refactoring a bunch of Selenium tests, this happens in the context of a generic action executor that operates on top of the Selenium API.) However, if I do something like:
WebElement applesOption = /* get apples option */
new Actions(webDriver).moveToElement(applesOption)
.click()
.perform();
Moving to the element throws the following exception:
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
I guess this is because elementsFromPoint() via the WebDriver Actions API seems to return a "non-finite" double for transparent elements like this?
Is there a way to prevent this from happening when using Actions? Maybe, in addition to checking if the element is clickable (ExpectedConditions#elementToBeClickable(...)), I would have to parse—which sounds horrible—attributes such as opacity?
I just tried your sample file locally and the code below is working with no exceptions.
WebElement e = driver.findElement(By.id("invisi_select"));
Select select = new Select(e);
select.selectByValue("apples");
System.out.println(select.getFirstSelectedOption().getText());
select.selectByValue("oranges");
System.out.println(select.getFirstSelectedOption().getText());
It prints
Apples
Oranges
This error message...
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
...implies that the WebDriver instance was unable to focus on the element for one or other reasons:
The element havn't loaded properly when you tried to interact with it.
The element haven't got the focus.
Solution
Instead of using the Java interactions API you can use the Select Class and you can use either of the following Locator Strategies:
Using cssSelector and selectByValue():
Select s = new Select(driver.findElement(By.cssSelector("select#invisi_select")));
s.selectByValue("apples");
Using xpath and selectByVisibleText():
Select s = new Select(driver.findElement(By.xpath("//select[#id='invisi_select']")));
s.selectByVisibleText("Apples");
References
You can find a couple of relevant detailed discussions in:
javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
I have following xpaths, which should be handled on same way by WebDriver, I need to get text content from these.
//*[#id="dialogMessage"]/div[3]
//*[#id="dialogMessage"]/div[3]/p
//*[#id="dialogMessage"]/div[3]/p/span[2]
I tried to use this code to match all of the above ones.
String result_text = driver.findElement(By.xpath("//*[contains(#value, 'dialogMessage')]")).getText();
Only one of these xpaths is present on page in each page loads. I get following error message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//id[contains(#value, 'dialogMessage')]"}
Looks like you're trying to get all elements that contain attribute value equals to dialogMessage instead try using this xpath:
//*[contains(#id, 'dialogMessage')]
or
//*[#id='dialogMessage']
Please see below attached the relevant XML code and my java code to locate the element:
Java code:
//Set date range
WebElement parentStartdate = obj.findElement(By.xpath("//[#id=typein_[Parameters].[Parameter 3]]"));
WebElement child=parentStartdate.findElement(By.xpath("//input[#value='1/1/2019']"));
I get the follow error:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[#id=typein_[Parameters].[Parameter 3]] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[#id=typein_[Parameters].[Parameter 3]]' is not a valid XPath expression.
Any help appreciated!
The whole xml structure:
If you have only one QueryBox on the webpage then you can select the element by using the following xpath for your case:
WebElement element = driver.findElement(By.xpath("//input[#class='QueryBox']"));
As the error message says, that is not a valid Xpath expression. Select the first element by
WebElement parentStartdate = obj.findElement(By.xpath("//span[#class='TypeInQuerySpan']/input"));
Try this selector:
//div[contains(#class, 'TypeInDiv')]/span[#class="TypeInQuerySpan"]/input[#type='text'][#class='QueryBox'][#value='1/1/2019']
the 3 identifiers on input are not mandatory, you can choose 1 or 2 or leave all 3, you decide.
I implemented the following line of code in Java. I am using Selenium and I am trying to get a web element by its text. As far as I know, this is not possible using css selectors.
What I have tried:
new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[contains(#class,'element_945')]//span[contains(text(),'Test')]"))
What I wanna do with the code above: get that span element that contains the text "Test" and is somewhere within the element div whose class contains "element_945".
The span is not a direct child of div but is somewhere inside of it.
When I run the code above in the Chrome console, it gives me the right element.
When I run it from my code, it gives me org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //div[contains(#class,'element_945')]//span[contains(text(),'Test')] (tried for 10 second(s) with 500 MILLISECONDS interval)
Thanks
All I am trying to go to Chinese sales force . and click on the products linkbyText using UTF. How do I do it? When I do via my method I get an exception.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.salesforce.com/cn/");
driver.findElement(By.linkText("\ufeff\u89e3\u51b3\u65b9\u6848")).click();
System.out.println("finish");
Error Message:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"link text","selector":"解决方案"}
Command duration or timeout: 1.08 seconds For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
It works perfectly fine if I use Chinese characters directly in the string.
driver.findElement(By.linkText("解决方案")).click();
Or use XPath:
driver.findElement(By.xpath(".//a[text()='解决方案']")).click();
However, the best would be matching href attribute instead of text.
driver.findElement(By.xpath(".//a[#href='/cn/solutions/']")).click(); // XPath version
driver.findElement(By.cssSelector("a[href='/cn/solutions/']")).click(); // CSS selector version
Its a common misconception that linktext method used href value to look for an element. Rather it needs just the text part of the link that is visible which in your case is chinese text.
driver.findElement(By.linkText("解决方案")).click();
In case some part of it is varible, you can use By.PartialLinkText method passing the constant part of the link text as parameter