如何进行注销的单击选项

时间:2016-01-12 09:15:41

标签: java selenium-webdriver

嗨,我是selenium webdriver的新手,有人帮我如何为下面的代码点击选项



<li id="signoutid" class="k-item k-state-default k-last" role="menuitem">
<a class="k-link" style="color: #1a1a1a;" onclick="javascript:window.open('/Account/Logout', '_self');">Logout</a>
</li>
&#13;
&#13;
&#13;

我在Xpath下面试过它不能正常工作

.//* [@ ID =&#39; signoutid&#39;] / A

dr.findElement(By.xpath(".//*[@id='signoutid']/a")).click();

及其显示元素不可见异常

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=47.0.2526.106)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20 milliseconds

3 个答案:

答案 0 :(得分:0)

根据提供的HTML代码,我期待您使用的xpath是错误的。 请尝试使用// [@class =&#39; k-link&#39;] [包含(text(),&#39;退出&#39;)]

driver.findElement(By.xpath("//a[@class='k-link'][contains(text(),'Logout')]")).click();

如果仍然无效,请尝试提供一些等待,并检查此元素是否在任何框架内。

由于

答案 1 :(得分:0)

首先,你的web元素的xpath是错误的。 正确的xpath应该像“// a [contains(text(),'Logout')]”

第一种方式

driver.findElement(By.xpath( “//一个[含有(文本(), '退出')]”)点击();

第二种方式

你也可以通过linkText找到这个元素

driver.findElement(By.linkText( “退出”));

现在,如果仍然无法看到网页元素

尝试使用

查明其是否可见

public boolean IsWebElementVisible(String xpathString){

int count = driver.findElement(By.xpath(“xpathString”))。size();

如果(计数大于0)    return true;

否则
   返回false;

}

答案 2 :(得分:0)

对不起,我给了时间等待代码正常工作

dr.findElement(By.xpath(&#34; .//* [@ ID =&#39; signoutid&#39;] / A&#34))。单击();