java - chrome developer tools XPath is unique but error is `NoSuchElementException` by selenium - Stack Overflow

I have the following html:<a id="basic-nav-dropdown" aria-expanded="false" role

I have the following html:

<a id="basic-nav-dropdown" aria-expanded="false" role="button" class="dropdown-toggle nav-link" tabindex="0" href="#">Stammdaten</a>

and find a unique occurrence in chrome dev tools:

(//*[@id="basic-nav-dropdown"])[1]

but when I use selenium as follows:

WebElement stammdaten = driver.findElement(By.xpath("(//*[@id='basic-nav-dropdown'])[1]"));

I get the error:

.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"(//*[@id='basic-nav-dropdown'])[1]"}

Any ideas?

I commented out driver.quit() in order to see the XPath of the element in the last opened browser window.

----------------considering first answer: thank you for your ideas!

  1. there is a tag nav with class="bg-white pb-2 ps-3 pe-3 shadow-sm d-flex justify-content-between navbar navbar-expand-md navbar-light" but I do not know, if it's shadow-dom? I cannot see any IFRAMEs.

  2. I added after driver.get(...) and the webapp is clearly visible in the opened browser:

  try {
            Thread.sleep(2000);  // Just for demonstration, use WebDriverWait in a real test
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
  1. different XPath has the same error as OP

I have the following html:

<a id="basic-nav-dropdown" aria-expanded="false" role="button" class="dropdown-toggle nav-link" tabindex="0" href="#">Stammdaten</a>

and find a unique occurrence in chrome dev tools:

(//*[@id="basic-nav-dropdown"])[1]

but when I use selenium as follows:

WebElement stammdaten = driver.findElement(By.xpath("(//*[@id='basic-nav-dropdown'])[1]"));

I get the error:

.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"(//*[@id='basic-nav-dropdown'])[1]"}

Any ideas?

I commented out driver.quit() in order to see the XPath of the element in the last opened browser window.

----------------considering first answer: thank you for your ideas!

  1. there is a tag nav with class="bg-white pb-2 ps-3 pe-3 shadow-sm d-flex justify-content-between navbar navbar-expand-md navbar-light" but I do not know, if it's shadow-dom? I cannot see any IFRAMEs.

  2. I added after driver.get(...) and the webapp is clearly visible in the opened browser:

  try {
            Thread.sleep(2000);  // Just for demonstration, use WebDriverWait in a real test
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
  1. different XPath has the same error as OP

Share edited Mar 7 at 16:30 Leder asked Mar 7 at 14:14 LederLeder 3941 gold badge6 silver badges23 bronze badges 2
  • Can you share the URL of the page if it's public? – Shawn Commented Mar 7 at 14:21
  • it's in intranet :-( – Leder Commented Mar 7 at 14:45
Add a comment  | 

2 Answers 2

Reset to default 2

With the given HTML, the XPath expression looks ok. Having said that, there could be more number of reasons for NoSuchElementExcpetion. Try few of the things below:

  1. Check for the presence of IFRAME or shadow-root surrounding the target element. If yes, you need to handle them first. If no, try point 2.

  2. Apply selenium's waits. Code for reference below:

     // Create WebDriverWait instance with a 10-second timeout
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    
     // Wait until the element is visible and store it in 'stammdaten'
     WebElement stammdaten = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//*[@id='basic-nav-dropdown'])[1]")));
    
  3. Try a different XPath expression as below:

     //a[@id="basic-nav-dropdown" and text()='Stammdaten']
    

I had two classes each with @Setup: ChromeDriver.
Now I made a WebDriverManager class with a singleton factory. That did the trick!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924382a4601340.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信