Search code examples
javaselenium-webdriverxpath

How to convert the object Text to Web Element?


I want to verify this text (CORN only) and already found its location by using Xpath

But when I tried the driver.findElementBy.xpath It said my xpath is an Object Text, not an element

Im using the selenium and java

My xpath is //address/br/following-sibling::node()[1][self::text()[normalize-space()="A19"]

(https://i.sstatic.net/EZfKo.png) (https://i.sstatic.net/9CowN.png)

So is there any way to convert this one? I don't know how to make it as an Element or to find an Object text through driver.

Please HELPPPP ME THANKS


Solution

  • You shouldn't construct complicated xpath and get second text, using it. Selenium can deal with WebElements, Corn is not WebElement, it's textContent of address element.

    Instead of it, you just need to get text of address element, split it and assert second element from array (or store it).

    String text = driver.findElement(new By.ByTagName("address")).getText();
    String[] address = text.split(System.lineSeparator());
    Assert.assertEquals(address[1], "Corn");