无法使用selenium找到元素,这是在MVC中开发的代码

时间:2016-07-19 07:59:35

标签: xpath selenium-webdriver

attached the code 我无法使用Xpath找到该元素。代码是在MVC框架中开发的,代码来自局部视图。请查看附图和帮助

<html class=" js flexbox no-touch backgroundsize boxshadow  
csstransforms" style="">
 <head>
<body>
<header class="header header-waterfall affix-top">
<div class="menu-wrapper pull-left" role="navigation">
<ul class="nav nav1" role="menubar">
<li role="menuitem">
<a href="#">
<span class="icon icon-lg">menu</span>
</a>
<div id="menu_nav" class="mega-menu" role="menu" aria-hidden="true" 
style="">
</li>
</ul>
</div>
<div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pull-left">
<ul class="nav nav-list pull-right" style="padding: 6px 0px; cursor: 
pointer;">
</header>

尝试使用spanli角色等。但是,最多次我收到错误,而不是互动。

  

失败:testCheckFarmerData   org.openqa.selenium.NoSuchElementException:无法找到元素:   {&#34;方法&#34;:&#34; xpath&#34;,&#34;选择器&#34;:&#34; // ul [@ class =&#39; nav nav 1&#39;] / LI /一个/跨度&#34;}   命令持续时间或超时:30.13秒

1 个答案:

答案 0 :(得分:0)

您应该尝试使用WebDriverWait,如下所示: -

WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("menu")));
el.click();

已修改: - 您可以使用click执行JavascriptExecutor,如下所示: -

WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("menu")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);

已编辑: -

WebDriverWait wait = new WebDriverWait(driver, 10); 
el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[@cla‌​ss='nav nav1']/descendant::span[contains(.,'menu')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);

希望它有所帮助.. :)