Cannot get element by text using Selenium and xPath - java

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

Related

How to click on image/button using java selenium chromedriver

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

Selenium WebDriver to match similar xpaths

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']

Salesforce application | Not able to click on a tab in tabbar using Selenium Webdriver

Need to click on 'Cases' tab in tab bar immediately after login.
Tried below xpath:
driver.findElement(By.xpath(".//*[#id='Case_Tab']/a")).click();
Error :
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath == .//*[#id='Case_Tab']/a (WARNING:
The server did not provide any stacktrace information) Command
duration or timeout: 328 milliseconds
For applications like Salesforce when you click on links it opens new tabs and each tab has the same 'close Tab' button attribute. So i am guessing you might have more than 1 tab opened and your trying to close a single Tab. Every time you write an xpath you need to check in the browser inspector(preferably Chrome) if unique elements are identified or not. If you have multiple elements matching with the xpath you should modify your xpath to find the unique.
Try below:
i) (//li[#id='Case_Tab'])[1] - Close Tab 1
ii) (//li[#id='Case_Tab'])[2] - Close Tab 2
And so on..
Although this is not the most efficient way of locating your element, Should atleast give you more clarity on your problem.

Unable to locate element in Selenium webdriver 2.0

I am unable to locate this element with the class name. Below is the HTML code:
<a class="j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear" title="Stream options" href="#"></a>
I tried to create an xpath using class and title both of them did the work in eclipse...ex:
//a[#title='Stream options']
//a[contains(#class,'j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear')]
..
the None of the above options worked and I tried a few others too...Essentially I want to click on this element and do some action..I want to locate the randomly created xpath so that I can click on the element in the next run.
FYI: the element is a hidden element I need to click on some other element before this element appears. This is a dynamically created element whose expath changes all the time.
Any suggestions would be greatly appreciated...Thanks
Is the element you want to select in a separate iframe? If so, you need to switch to the correct iframe (driver.switchTo().frame("frame-id")) before firing the xpath selector.
Additionally, something to watch out for is that old versions of IE didn't have a native xpath library. See this answer for more details.

Unable to click on Chinese linkText on Salesforce Chinese version

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

Categories