有人能帮我找一下第五个'li‘元素吗?因为那个接收错误时试图定位它。以下是页面上的外观:
<ul class="navigation-primary navigation-primary--right js-navigation-primary">
<li><a data-modal="login-modal" href="javascript:void(0);" data-modal-content-switch="login-options" class="is-button-group-right js-prevent-trigger modal-content-button"><span>Logga in</span></a></li>
<li><a href="javascript:void(0)" class="icon icons8-Search has-no-border-bottom" data-menu="search"></a></li>
<li><a href="javascript:void(0)" class="has-no-border-bottom" data-menu="language">Svenska</a></li>
实际上,我需要最后一个具有以下属性的
data-menu="language"因为有几种语言--我想它可以用if循环来解决:
if (driver.findElement(By.xpath("//*[@id="header"]/div[2]/div/ul[2]/li[5]/a") != null
driver.findElement(By.xpath("//*[@id="header"]/div[2]/div/ul[2]/li[5]/a").click();
else {
system.out.println("element not present");
}因为有几种语言,而且每一种语言都有li5 --认为lang的名字可以解决这个问题,但是没有找到解决方案。提前谢谢你
发布于 2017-10-16 19:09:31
冒着重新定义您的问题的风险,我会尝试将一些id标记添加到您的html中并调用driver.findElement(By.id(name))。
好处是,如果在列表的开头添加了一个额外的项,那么您的测试就不会中断。
https://stackoverflow.com/questions/46771130
复制相似问题