我是Selenium-WebDriver的新手。试图找到一个元素并单击它。

但是不断出现错误:


  无法找到元素


Firepath提供了以下xpath:

"xpath = html/body/header/div/ul/li[2]/a/span[1]"


相关的HTML代码是:

<ul class="nav navbar-nav navbar-right top-nav ">
<li class="identity has-icon">
<a href=".......">
</li>
<li class="settings has-icon">
<a href=".......">
<span class="icon icon-cogs" aria-hidden="true" role="presentation"/>
**<span class="nav-title">Settings</span>**
</a>
</li>


我尝试通过以下方式查找元素:

1. driver.findElement(By.xpath("//div[@id='right-nav']//a[contains(text(),'Settings')]")).click();

2.  driver.findElement(By.xpath("//div[@id='nav-title']//a[contains(text(),'Settings')]")).click();

3. driver.findElement(By.xpath("//div[contains(@class, 'nav-title')]")).click();


有人可以帮我找到解决方案吗?

最佳答案

它在那里不需要contains,并且您的所有语句在xpath中也两次包含//,这是不正确的。

请使用以下声明

//@ is used to get the span class which is nav-title and
// whose span text is Settings
driver.findElement(By.xpath("//li/a/span[@class ='nav-title' and . = 'Settings']")).click();

关于java - Selenium Web驱动程序-无法找到元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38005286/

10-09 07:18
查看更多