如何解决,org.openqa.selenium element.nosuchelementexception

org.openqa.selenium.support.ui.ExpectedConditions
Start line: &
End line: &
Snippet Preview
Snippet HTML Code
&&&&&&&&&&&&&&&&&&&&&&&&&&&&package&org.openqa.selenium.support.&&import&;&import&;&import&;&import&;&import&;&import&;&import&;&&import&;&import&;&import&; Canned s which are generally useful within webdriver
&&public&class&&{&&&&private&final&static&&&=&Logger.(.class.());&&&&private&()&{&&&&&&&&}&& An expectation for checking the title of a page.
&&&&public&static&&>&(final&&title)&{&&&&&return&new&&>()&{&&&&&&&public&&(&driver)&{&&&&&&&&&return&title.(driver.());&&&&&&&}&&&&&};&&&}&& An expectation for checking that the title contains a case-sensitive
&&&&public&static&&>&(final&&title)&{&&&&&return&new&&>()&{&&&&&&&public&&(&driver)&{&&&&&&&&&&currentTitle&=&driver.();&&&&&&&&&return&currentTitle&==&null&?&false&:&currentTitle.(title);&&&&&&&}&&&&&};&&&}&& An expectation for checking that an element is present on the DOM of a
page. This does not necessarily mean that the element is visible.
&&&&public&static&&>&(&&&&&&&final&&locator)&{&&&&&return&new&&>()&{&&&&&&&public&&(&driver)&{&&&&&&&&&return&(locator,&driver);&&&&&&&}&&&&&};&&&}&& An expectation for checking that an element is present on the DOM of a page
and visible. Visibility means that the element is not only displayed but
also has a height and width that is greater than 0.
&&&&public&static&&>&(&&&&&&final&&locator)&{&&&&return&new&&>()&{&&&&&&public&&(&driver)&{&&&&&&&&try&{&&&&&&&&&&return&((locator,&driver));&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& An expectation for checking that an element, known to be present on the DOM
of a page, is visible. Visibility means that the element is not only
displayed but also has a height and width that is greater than 0.
&&public&static&&>&(&&&&&&final&&element)&{&&&&return&new&&>()&{&&&&&&public&&(&driver)&{&&&&&&&&return&(element);&&&&&&}&&&&};&&}&& &&private&static&&(&element)&{&&&&return&element.()&?&element&:&null;&&}&& An expectation for checking that there is at least one element present on a
&&public&static&&&>>&(&&&&&&final&&locator)&{&&&&return&new&&&>>()&{&&&&&&public&&>&(&driver)&{&&&&&&&&return&(locator,&driver);&&&&&&}&&&&};&&}&& An expectation for checking if the given text is present in the specified
&&public&static&&>&(&&&&&&final&&locator,&final&&text)&{&&&&return&new&&>()&{&&&&&&public&&(&from)&{&&&&&&&&try&{&&&&&&&&&&&elementText&=&(locator,&from).();&&&&&&&&&&return&elementText.(text);&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& An expectation for checking if the given text is present in the specified
elements value attribute.
&&public&static&&>&(&&&&&&final&&locator,&final&&text)&{&&&&return&new&&>()&{&&&&&&public&&(&from)&{&&&&&&&&try&{&&&&&&&&&&&elementText&=&(locator,&from).("value");&&&&&&&&&&if&(elementText&!=&null)&{&&&&&&&&&&&&return&elementText.(text);&&&&&&&&&&}&else&{&&&&&&&&&&&&return&false;&&&&&&&&&&}&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& An expectation for checking whether the given frame is available to switch
If the frame is available it switches the given driver to the
specified frame.
&&public&static&&>&(&&&&&&final&&frameLocator)&{&&&&return&new&&>()&{&&&&&&public&&(&from)&{&&&&&&&&try&{&&&&&&&&&&return&from.().(frameLocator);&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& An Expectation for checking that an element is either invisible or not
present on the DOM.
&&public&static&&>&(&&&&&&final&&locator)&{&&&&return&new&&>()&{&&&&&&public&&(&driver)&{&&&&&&&&try&{&&&&&&&&&&return&!((locator,&driver).());&&&&&&&&}&catch&(&e)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&return&true;&&&&&&&&}&catch&(&e)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&return&true;&&&&&&&&}&&&&&&}&&&&};&&}&& An Expectation for checking an element is visible and enabled such that you
can click it.
&&public&static&&>&(&&&&&&final&&locator)&{&&&&return&new&&>()&{&&&&&&public&&>&&=&&&&&&&&&&ExpectedConditions.(locator);&&&&&&public&&(&driver)&{&&&&&&&&&element&=&.(driver);&&&&&&&&try&{&&&&&&&&&&if&(element&!=&null&&&&element.())&{&&&&&&&&&&&&return&element;&&&&&&&&&&}&else&{&&&&&&&&&&&&return&null;&&&&&&&&&&}&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& Wait until an element is no longer attached to the DOM.
&&public&static&&>&(&&&&&&final&&element)&{&&&&return&new&&>()&{&&&&&&public&&(&ignored)&{&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&element.();&&&&&&&&&&return&false;&&&&&&&&}&catch&(&expected)&{&&&&&&&&&&return&true;&&&&&&&&}&&&&&&}&&&&};&&}&& An expectation for checking if the given element is selected.
&&public&static&&>&(final&&element)&{&&&&return&(element,&true);&&}&& An expectation for checking if the given element is selected.
&&public&static&&>&(final&&element,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&final&boolean&selected)&{&&&&return&new&&>()&{&&&&&&public&&(&from)&{&&&&&&&&return&element.()&==&selected;&&&&&&}&&&&};&&}&&public&static&&>&(final&&locator)&{&&&&return&(locator,&true);&&}&&public&static&&>&(final&&locator,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&final&boolean&selected)&{&&&&return&new&&>()&{&&&&&&public&&(&from)&{&&&&&&&&try&{&&&&&&&&&&&element&=&from.(locator);&&&&&&&&&&return&element.()&==&selected;&&&&&&&&}&catch&(&e)&{&&&&&&&&&&return&null;&&&&&&&&}&&&&&&}&&&&};&&}&& Looks up an element. Logs and re-throws WebDriverException if thrown.
Method exists to gather data for /p/selenium/issues/detail?id=1800
&&private&static&&(&by,&&driver)&{&&&&try&{&&&&&&return&driver.(by);&&&&}&catch&(&e)&{&&&&&&throw&e;&&&&}&catch&(&e)&{&&&&&&.(.,&&&&&&&&&&String.("WebDriverException&thrown&by&findElement(%s)",&by),&e);&&&&&&throw&e;&&&&}&&}&& &&private&static&&>&(&by,&&driver)&{&&&&try&{&&&&&&return&driver.(by);&&&&}&catch&(&e)&{&&&&&&.(.,&&&&&&&&&&String.("WebDriverException&thrown&by&findElement(%s)",&by),&e);&&&&&&throw&e;&&&&}&&}}
&org.openqa.selenium.support.ui
<div class="gae-status" id="gae-status-repo1.maven.org$maven2@org.seleniumhq.selenium$selenium-support@2.19.0@org$openqa$selenium$support$ui@ExpectedConditions@()">
New to GrepCode? Check out ourorg.openqa.selenium.NoSuchElementException:
时间: 12:57:42
&&&& 阅读:2714
&&&& 评论:
&&&& 收藏:0
标签:http://www.blogjava.net/qileilove/archive//421309.html
webdriver定位不到元素的五种原因及解决办法
  1.动态id定位不到元素
  for example:
  //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
  WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,‘写 信‘)]"));
  xiexin_element.click();
  上面一段代码注释掉的部分为通过id定位element的,但是此id&_mail_component_82_82&后面的数字会随着你每次登陆而变化,此时就无法通过id准确定位到element。
  所以推荐使用xpath的相对路径方法查找到该元素。
 2.iframe原因定位不到元素
  由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素
  比如以下一段xml源文件:
&iframe id="left_frame"
scrolling="auto" frameborder="0" src="index.php?m=Index&a=Menu"
name="left_frame" noresize="noresize" style="height: 100%;visibility:
width: 100%;z-index: 1"&
&!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&
class="menuBg"&
id="menu_node_type_0"&
&table width="193"
cellspacing="0" cellpadding="0" border="0"&
&td id="c_1"&
&table class="menuSub"
cellspacing="0" cellpadding="0" border="0" align="center"&
class="sub_menu"&
href="index.php?m=Coupon&a=SearchCouponInfo"
target="right_frame"&密码重置&/a&
  原本可以通过
  WebElement
element = driver.findElement(By.linkText("密码重置"));
  来定位此元素,但是由于该元素在iframe
id="left_frame"这个frame里边
所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素
  WebElement
=driver.switchTo().frame("left_frame").findElement(By.linkText("密码重置"));
  3.不在同一个frame里边查找元素
  大家可能会遇到页面左边一栏属于left_frame,右侧属于right_frame的情况,此时如果当前处在
  left_frame,就无法通过id定位到right_frame的元素。此时需要通过以下语句切换到默认的content
  driver.switchTo().defaultContent();
  例如当前所在的frame为left_frame
  WebElement
xiaoshoumingxi_element =
driver.switchTo().frame("left_frame").findElement(By.linkText("销售明细"));
  xiaoshoumingxi_element.click();
  需要切换到right_frame
  driver.switchTo().defaultContent();
  Select
quanzhong_select2 = new
Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));
  quanzhong_select2.selectByVisibleText("售后0小时");
xpath描述错误
  这个是因为在描述路径的时候没有按照xpath的规则来写
造成找不到元素的情况出现
  5.点击速度过快
页面没有加载出来就需要点击页面上的元素
  这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait
和util来实现
  例如:
  //用WebDriverWait和until实现显示等待
等待欢迎页的图片出现再进行其他操作
  WebDriverWait
wait = (new WebDriverWait(driver,10));
  wait.until(new
ExpectedCondition&Boolean&(){
  public
Boolean apply(WebDriver d){
  boolean
loadcomplete =
d.switchTo().frame("right_frame").findElement(By.xpath("//center/div[@class=‘welco‘]/img")).isDisplayed();
  return
  也可以自己预估时间通过Thread.sleep(5000);//等待5秒
这个是强制线程休息
  6.firefox安全性强,不允许跨域调用出现报错
  错误描述:uncaught
exception: [Exception... "Component returned failure code: 0x
(NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x
(NS_ERROR_FAILURE)" location:
  解决办法:
  这是因为firefox安全性强,不允许跨域调用。
  Firefox
要取消XMLHttpRequest的跨域限制的话,第一
about:config 里设置 signed.applets.codebase_principal_support = true;
(地址栏输入about:config 即可进行firefox设置)
  第二就是在open的代码函数前加入类似如下的代码:
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }
catch (e) { alert("Permission UniversalBrowserRead denied."); }
  最后看了乙醇的文章
import java.io.F
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebD
importorg.openqa.selenium.chrome.ChromeD
importorg.openqa.selenium.support.ui.ExpectedC
importorg.openqa.selenium.support.ui.WebDriverW
public classButtonDropdown
public static
voidmain(String[] args) throws InterruptedException {
WebDriver dr =
newChromeDriver();
File file =
newFile("src/button_dropdown.html");
String filePath =
"file:///" + file.getAbsolutePath();
System.out.printf("nowaccesss %s
\n", filePath);
dr.get(filePath);
Thread.sleep(1000);
定位text是watir-webdriver的下拉菜单
首先显示下拉菜单
dr.findElement(By.linkText("Info")).click();
(newWebDriverWait(dr,
10)).until(new ExpectedCondition&Boolean&(){
Booleanapply(WebDriver d){
returnd.findElement(By.className("dropdown-menu")).isDisplayed();
通过ul再层级定位
dr.findElement(By.className("dropdown-menu")).findElement(By.linkText("watir-webdriver")).click();
Thread.sleep(1000);
System.out.println("browser
will be close");
dr.quit();
  然后我自己定位的。
public XiaoyuanactivityPage
zipaixiuye(){
driver.navigate().refresh();
luntan.click();
WebDriverWrapper.waitPageLoad(driver,3);
(new WebDriverWait(driver,
10)).until(newExpectedCondition&Boolean&() {
public Boolean
apply(WebDriverdriver){
returndriver.findElement(By.className("TFB_sub_li")).isDisplayed();
driver.findElement(By.className("TFB_sub_li")).findElement(By.linkText("自拍秀")).click();
returnPageFactory.initElements(this.getDriver(),
XiaoyuanactivityPage.class);
&&国之画&&&& &&&&chrome插件&&
版权所有 京ICP备号-2
迷上了代码!Selenium3报错之org.openqa.selenium.NoSuchElementException
最近工作闲暇时间开始自学Selenium,买了本书以便有学习的思路,于是就跟着书中章节开始一步步练习。但就是在练习过程中,发现有几个实例明明写的没有问题,但执行总是报错,日志显示找不到对应的元素,后来频繁更换定位方式后仍是无果,实在是百思不得其解。于是各种百度,研究了大半天终于知道原因了,哭笑不得,现将问题记录如下:
问题描述:编写java脚本实现打开126邮箱进行登录操作,执行结果总是提示:
Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to locate
element: //*[@id='loginDiv']/iframe
页面代码:
Java代码片段:
运行报错截图:
问题分析:
这个报错意思是找得到元素,但是它不在当前可见的页面上。可能性很多:
1. 上个操作和下个操作间隔时间太短,代码执行速度比页面渲染速度快。
2. 分辨率问题,运行环境驱动与调试的驱动不一样。
可行建议:用try的方法,失败的时候通过webdirver截一下图看是什么情况。
经尝试增加思考时间,问题解决。
备注:转载请注明出处,谢谢。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 nosuchfileexception 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信