70381

Cannot get text using text() in XPath

Question:

Here is part of html code:

<pre class="snippet-code-html lang-html prettyprint-override"><td class="audit-context-break-word text-left" nowrap="nowrap"> <b class="ng-binding">PONumber</b></td> <td class="audit-context-break-word text-left ng-binding">20202022 02_001 </td>

I need to get only text inside (expected value: 20202022 02_001) I tried following variants:

By.xpath("/descendant::*[.='PONumber']/../descendant::*[@class=\"audit-context-break-word text-left ng-binding\"]/text()") By.xpath("/descendant::*[.='PONumber']/../following::text()[1]")

The issue is that in FirePath selected text is found, but not in test. It is failed due to timeout exception, because element is not found

What is the reason of the failure?

Answer1:

You can use following-sibling to write the Xpath.

WebElement element=driver.findElement(By.xpath("//td[b[contains(text(),'PONumber')]]/following-sibling::td")); String text = element.getText(); System.out.println(text); <ol><li>

I'm identifying appropriate web element on the webpage and saving it in web element.

</li> <li>

Using getText() method to obtain text from that web Element and saving it in string

</li> <li>

Print that string text

</li> </ol>

Answer2:

<a href="https://developer.mozilla.org/en-US/docs/Web/XPath/Axes/following-sibling" rel="nofollow">following-sibling</a> is something you should use:

driver.findElement(By.xpath("//td[b = 'PONumber']/following-sibling::td")).getText();

Answer3:

Selenium cannot evaluate text() function of xpath. you can run javascript and can get result. run following javascript in your program:

document.evaluate("//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]", document, null, XPathResult.STRING_TYPE, null).stringValue;

if you use Python or Java, i can present

Answer4:

I had a similar problem where an element was visible but tests wouldn't pass on isTextPresent. When I checked the source code, it was

<li>2-1180 \&\nbsp CITY 2</li>, whereas the test was looking for "2-1180 CITY 2". Check if you have a similar problem too. Let us know what works for you, would be interested in knowing.

Note: do not downvote, just trying to help.

Recommend

  • Selenium WebDriver HTML Code
  • Selenium, Xpath hidden by Js
  • can't source script in a current directory
  • Build MPICH2 from source
  • Decompile *.pyc file using uncompyle2
  • why does bash tell me that the command if is not found when I run the following code?
  • passing parameter to server in ExtJs
  • twisted.internet.error.ConnectError when run scrapy spider
  • Extjs, handling success or failure when doing a standard submit in a form
  • How to implement simple validation in Scala
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • android duplicate provider authority on apps that don't have provider
  • unrecognized selector isPitched called
  • Zend framework 2 : Add different authentication adapter for two different modules
  • JavaScript IE rotation transform maths
  • C# - Most efficient way to iterate through multiple arrays/list
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Image map in Flex
  • Who propagate bugfixes across branches (corporate development)?
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Cannot upload to OneDrive using the new SDK
  • MySQL Order by column = x, column asc?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Debug.DrawLine not showing in the GameView
  • Yii2: Config params vs. const/define
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • javascript inside java/jsp code
  • Hazelcast - OperationTimeoutException
  • Weird JavaScript statement, what does it mean?
  • Jquery - Jquery Wysiwyg return html as a string
  • SetUp method failed while running tests from teamcity
  • File upload with ng-file-upload throwing error
  • Android Studio and gradle
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Django query for large number of relationships
  • How can i traverse a binary tree from right to left in java?
  • How to push additional view controllers onto NavigationController but keep the TabBar?