1

I have an HTML with the following element:

<a href="index.html" style="text-transform: uppercase;">index</a>

I'm using selenium-java 4.8.2, Chrome 112 and JUnit 5.9.2, and trying to find this element via WebDriver's linkText locator.

public void testMethod() {
    driver.get("<myUrl>");
    driver.findElement(By.linkText("index")).click();
}

And it does not work. After some investigation I found that when I replace By.linkText("index") with By.linkText("INDEX"), it works, so it looks like the text-transform CSS styling is applied before the element is found.

On the other hand, using the good old XPath works.

driver.findElement(By.xpath("//a[text()='index']")).click();

Does anyone have an idea on this topic? I thought that CSS transformations should not be applied when finding an element using any selector strategy using WebDriver, but I could not find any explicit information regarding this topic.

1 Answer 1

0

Yes, linkText is case-sensitive and hence you see NoSuchElementException

There are 2 ways to avoid CSS pseudo elements -

  1. Use XPath to find your elements
  2. Use Javascript executor (refer article - https://www.lambdatest.com/blog/handling-pseudo-elements-in-css-with-selenium/#:~:text=How%20To%20Work%20With%20Pseudo%2DElements%20In%20Selenium%20Java%3F )

In your case, if you are confident the CSS transformation won't change. You may use

driver.findElement(By.linkText(text.toUpperCase()));

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.