Xpath 无法找到使用selenium的元素,代码是在MVC中开发的
我无法使用Xpath 无法找到使用selenium的元素,代码是在MVC中开发的,xpath,selenium-webdriver,Xpath,Selenium Webdriver,我无法使用Xpath找到元素。代码是在MVC框架中开发的,代码来自局部视图。请检查附加的图像和帮助 <html class=" js flexbox no-touch backgroundsize boxshadow csstransforms" style=""> <head> <body> <header class="header header-waterfall affix-top"> <div class="menu-wrap
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>
-
尝试使用span
,li
角色等。但是,出现错误的次数最多,例如,未交互
失败:testCheckFarmerData
org.openqa.selenium.NoSuchElementException:无法定位元素:
{“方法”:“xpath”,“选择器”:”//ul[@class='nav nav 1']/li/a/span}
命令持续时间或超时:30.13秒
您应该尝试使用
WebDriverWait
,如下所示:-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("menu")));
el.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("menu")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
Edited1:-您可以使用JavascriptExecutor
执行单击
,如下所示:-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("menu")));
el.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("menu")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
编辑2:-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[@class='nav nav1']/descendant::span[contains(.,'menu')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
希望对您有所帮助。:)与您分享您尝试过的selenium代码……您说找不到元素是什么意思??。。有什么例外吗??请分享它也请找到附加的图像硒代码。尝试使用span、li角色等,但最多出现错误次数为,未交互。失败:testCheckFarmerData org.openqa.selenium.NoSuchElementException:找不到元素:{“方法”:“xpath”,“选择器”:“//ul[@class='nav nav 1']/li/a/span”}命令持续时间或超时:30.13秒他说,用尽一切办法,找到了开发人员,它是在MVC框架下开发的,顶部菜单是从部分视图中提取的!它终于开始工作了。从2天开始,Sit不在FF中工作,有时也在chrome中工作。它只在室内工作IE@SuneelKuppili你什么意思它不在FF中工作,有时也在chrome中工作??是否有例外情况???@SuneelKuppili如果有超时,您需要将时间从10秒增加到更多。我再次遇到同样的错误,我尝试将时间增加到100毫秒。这是错误:失败:testCheckFarmerData org.openqa.selenium.ElementNotVisibleException:元素不可见(会话信息:chrome=51.0.2704.103)(驱动程序信息:chromedriver=2.22.397933(1cab651507b88dec79b2b2a22d1943c01833cc1b),平台=Windows NT 6.1.7601 SP1 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:20毫秒