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
c32hedge
2,70920 silver badges39 bronze badges
asked Apr 12, 2018 at 8:51

5 Answers 5

3

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;");
answered Apr 12, 2018 at 12:58
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]

answered Apr 12, 2018 at 9:05
3
  • 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: Commented Apr 12, 2018 at 9:43
  • Edited my answer - my bad. You need [] after the *. Commented Apr 12, 2018 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 Commented Apr 12, 2018 at 12:56
0

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

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

answered Jun 29, 2021 at 17:26
0

You can try with below locators: xapth: //h4[a[contains(text(),'Load #')]]

enter image description here

css: .load-number-link

enter image description here

and you will get the collection of WebElements. Please use getText().trim(). You will get the text. After this you can use .split() method. This will split the text into an array e.g. {"Load #", "779999"}. There you can get it at index 1 This solution is tried and tested. Enjoy :)

Please note that we should not go for JavascriptExecutor until and unless there is no workaround. Here complete solution provided is using Selenium and Programming only (JAVA)

answered Jul 1, 2021 at 14:32
-1

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

enter image description here

answered Oct 8, 2019 at 17:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.