-1

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 an 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

How i can resolve plz help its urgent..

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 quite a tricky workaround I have prepared:

First we're setting up a helper method that would wrap a particular "strong" item to the xPath:

public static String getXpathForTitle(String title){
    return "//div[@class='equipment_item']//text()[preceding-sibling::strong[text()='" + title + "']][1]";
}

Not a rocket sience yet. Then we take a pretty tricky step. Yet another point to my answer for Why should a software QA engineer need to learn JavaScript?

Now we should prepare some JavaScript code that would wrap a text node into a WebElement (for example span element). Not to break the structure we will just clone the text taken by our prepared xPath, wrap it in span and add to the end of body element.

Another important aspect is that when we create a new element we assign a unique id to it:

public static String injectWrappedText(String xpath, WebDriver driver){
    String randomId = UUID.randomUUID().toString();
    StringBuilder script = new StringBuilder();
    script
            .append("var webElementWrapper = document.createElement(\"span\");").append("\n")
            .append("var text = document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.cloneNode(true);").append("\n")
            .append("var body = document.getElementsByTagName(\"body\");").append("\n")
            .append("webElementWrapper.appendChild(text);").append("\n")
            .append("body.item(0).appendChild(webElementWrapper);").append("\n")
            .append("var att = document.createAttribute(\"id\");").append("\n")
            .append("att.value = \""+randomId+"\";").append("\n")
            .append("webElementWrapper.setAttributeNode(att);");
    ((JavascriptExecutor)driver).executeScript(script.toString());
    return randomId;
}

So the method above takes the xpath, clone the text wrapping it to a WebElement, generates uiniqe id for it, adds that new element to DOM and returns that id.

Now we can just use all that stuff to parse the values from the required places as it is shown below:

public static void main(String[] args) {

    System.setProperty("phantomjs.binary.path", "C:\\Temp\\phantomjs-2.1.1-windows\\bin/phantomjs.exe");
    WebDriver driver = new PhantomJSDriver();
    driver.get("http://carrier.veritread.com/Details?LoadId=f9a1c81e-151b-48d7-afea-6a9aa2be2cfd");

    String dateListedId = injectWrappedText(getXpathForTitle("Date Listed:"), driver);
    String loadId = injectWrappedText(getXpathForTitle("Load #:"), driver);
    System.out.println(driver.findElement(By.id(dateListedId)).getText());
    System.out.println(driver.findElement(By.id(loadId)).getText());
    driver.quit();
}

Output:

4/10/2018

276948

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]

  • 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

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.