0

Hello I am trying to find Load # detail from this site. Also You can refer screenshot too.

I have tried with xpath: //div[@class='equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1]

But getting the below error:

org.openqa.selenium.InvalidSelectorException: The given selector //div[@class=' equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression "//div[@class=' equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1]" is: [object Text]. It should be an element.
Command duration or timeout: 31 milliseconds
2

Since a text node is not a Webelement there is no way to pick a single text node with Selenium (there is a dedicated method getText() that returns all the text from the WebElement which is not flexible enough).

Here is the updated simple way how to find a value of single text node (see previous way in answer history):

JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
String value = (String)javascriptExecutor.executeScript("document.evaluate(\"//div[@class='equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1]\", document, null, XPathResult.STRING_TYPE, null ).stringValue;");
| improve this answer | |
0

You probably want an element having the text. So you are missing an asterisk before the first text(), and enclose the text() part in another set of brackets. Let us know if that works or fixes that error at least.

//div[@class='equipment_item']//*[text()[preceding-sibling::strong[text()='Load #:']]][1]

| improve this answer | |
  • I have tried but still getting an error org.openqa.selenium.InvalidSelectorException: The given selector //div[@class=' equipment_item']//*text()[preceding-sibling::strong[text()='Load #:']][1] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //div[@class='equipment_item']//*text()[preceding-sibling::strong[text()='Load #:']][1] because of the following error: – manish kumar Apr 12 '18 at 9:43
  • Edited my answer - my bad. You need [] after the *. – FDM Apr 12 '18 at 11:56
  • still not able to find element getting an error : org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[@class='listing-info']//*[text()[preceding-sibling::strong[text()='Load #:']]][1]"} Command duration or timeout: 16 milliseconds Please take look how can resolve – manish kumar Apr 12 '18 at 12:56
0

Try this solution. Works perfectly in Google Chrome dev tools console.

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.