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!
there is a tag
nav
withclass="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.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();
}
- 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!
there is a tag
nav
withclass="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.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();
}
- 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
2 Answers
Reset to default 2With 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:
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.
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]")));
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条)