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
5 Answers 5
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;");
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]
-
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– manish kumar2018年04月12日 09:43:27 +00:00Commented Apr 12, 2018 at 9:43
-
Edited my answer - my bad. You need [] after the *.FDM– FDM2018年04月12日 11:56:33 +00:00Commented 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 resolvemanish kumar– manish kumar2018年04月12日 12:56:30 +00:00Commented Apr 12, 2018 at 12:56
Replace this "//div[@class=' equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1]"
With "//div[@class=' equipment_item'][preceding-sibling::strong[text()='Load #:']][1]"
You can try with below locators: xapth: //h4[a[contains(text(),'Load #')]]
css: .load-number-link
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)
Try this solution. Works perfectly in Google Chrome dev tools console.
Explore related questions
See similar questions with these tags.