【问题标题】:Unable to find the element using selenium, code developed in MVC无法使用 selenium 找到元素,在 MVC 中开发的代码
【发布时间】:2016-07-19 07:59:34
【问题描述】:

attached the code 我无法使用Xpath 找到元素。代码是在MVC框架下开发的,代码来自partial-view。请检查附图并帮助

<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:无法定位元素: {"method":"xpath","selector":"//ul[@class='nav nav 1']/li/a/span"} 命令持续时间或超时:30.13 秒

【问题讨论】:

  • 分享你尝试过的硒代码......
  • 你说的找不到元素是什么意思???...有什么例外吗??也请分享一下
  • 请找到 selenium 代码的附件图像。尝试使用 span、li 角色等。但是,我得到错误的最大次数是,没有交互。失败:testCheckFarmerData org.openqa.selenium.NoSuchElementException:无法定位元素:{"method":"xpath","selector":"//ul[@class='nav nav 1']/li/a/span" } 命令持续时间或超时:30.13 秒
  • 千方百计联系了开发者,他说,它是在MVC框架下开发的,顶部菜单是从Partial-view拉出来的

标签: xpath selenium-webdriver


【解决方案1】:

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

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

Edited1 :- 您可以使用JavascriptExecutor 执行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[@cla‌​ss='nav nav1']/descendant::span[contains(.,'menu')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);

希望对你有帮助..:)

【讨论】:

  • 太棒了!它终于开始工作了。挣扎了 2 天
  • 它有时在 FF 中也不能在 chrome 中工作。它只在 IE 中工作
  • @SuneelKuppili 你是什么意思它在 FF 中也不能在 chrome 中工作?有例外吗??
  • @SuneelKuppili 如果有timeOut,你需要将你的时间从10秒增加到更多..
  • 我再次遇到同样的错误,我尝试将时间增加到 100 毫秒。这是错误:失败:testCheckFarmerData org.openqa.selenium.ElementNotVisibleException:元素不可见(会话信息:chrome=51.0.2704.103)(驱动程序信息:chromedriver=2.22.397933(1cab651507b88dec79b2b2a22d1943c01833cc1b),平台=Windows NT 6.1.760 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:20 毫秒
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 2013-09-16
  • 1970-01-01
  • 2018-08-01
  • 2020-08-28
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多