0

I'm trying to learn Selenium on Java, can't for the life of me figure out why xpath contain can't find the right element.
Here's the whole case:

@Test
public void test1_FluentWait() {
    driver.get("https://www.google.com");
    WebElement searchBar = driver.findElement(By.xpath("//input[contains(@name, 'q')]"));
    searchBar.sendKeys("Selenium", Keys.ENTER);

    Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofSeconds(30))
            .pollingEvery(Duration.ofSeconds(5))
            .ignoring(NoSuchElementException.class);
    
    WebElement element = fluentWait.until(new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver w) {
            // WebElement ele = w.findElement(By.xpath("//*[contains(text(), 'Selenium - Health Professional')]")); // Doesn't work, no idea why
            WebElement ele = w.findElement(By.partialLinkText("Selenium - Health Professional")); // works
            // WebElement ele = w.findElement(By.xpath("//*[text()='Selenium - Health Professional']"));
            // WebElement ele = w.findElement(By.xpath("//*[contains(@href, 'Selenium-HealthProfessional')]")); // Found attribute, can't click
            if (ele.isEnabled()) {
                System.out.println("Found");
            }
            return ele;
        }
    });
    System.out.println(element.getText());
    // element.click();
}

Here's the element in question Google Search's element

So basically, only the partialLinkText works when finding this element.

WebElement ele = w.findElement(By.xpath("//*[contains(text(), 'Selenium - Health Professional')]")); // Doesn't work, no idea why
WebElement ele = w.findElement(By.partialLinkText("Selenium - Health Professional")); // works
WebElement ele = w.findElement(By.xpath("//*[text()='Selenium - Health Professional']"));

contains text() prints "found" but the returned element's getText() prints empty string. click() throws ElementNotInteractableException
text()='...' throws NoSuchElementException
partialLinkText returns the element and allows click()

What am I doing wrong? What is the issue here? How can I prevent this or know in the future which findElement to use?

1 Answer 1

0

There is a possibility that there are found more than one webelement which are matching the xpath criteria. A way to find it out is use findElements with xpath and check the count of list.

when we use findElement the first match is returned, which could be an element which is not workable or the link.

When you use partialLinkText it knows it has to find a link and match with partial text.

please check this.

hope it helps.

4
  • I will check this, I did notice that there’s a lot more use case to use findElements such as checking if element exist. Is it good practice though, to use findElements for many things I expect 1 result for?
    – Eddie
    Commented Apr 15, 2022 at 19:51
  • At first you should be able to find this information about an element on the web page using a locator you are trying to find the element. And then if you see there are more elements found, either use a locator which help you return the element you wish, and use findElements method if you wish to act on all elements found.
    – Pallavi
    Commented Apr 16, 2022 at 5:11
  • Thanks, Pallavi, you're right, it found 3 elements instead of 1. That was the problem. Third element was the right one.
    – Eddie
    Commented Apr 20, 2022 at 21:08
  • good to know this Eddie
    – Pallavi
    Commented Apr 21, 2022 at 4:56

Your Answer

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

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