【发布时间】:2015-08-27 14:43:56
【问题描述】:
我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉。
我正在使用 Selenium 和 Java 编写测试。但是在寻找元素方面存在问题。我知道一些其他方法可以找到这个 WebElement,
但为什么会这样:
WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));
无法得到这个:
<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>
并显示此错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no
such element: Unable to locate element:{"method":"xpath","selector":"
//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"}
这行得通:
WebElement we1 = driverChrome.findElement(By
.xpath("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));
但这些不起作用:
WebElement we1 = driverChrome.findElement(By
.xpath("//div[contains(@class,'ui-draggable') and @title='project.CPG']"));
WebElement we1 = driverChrome.findElement(By.
xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
听起来很奇怪,这是:
WebElement we = driverChrome.findElement(By
.xpath("//div[contains(@class,'combine-red')]"));
只是为此工作:
<div class="leaflet-marker-icon combine-red-off leaflet-zoom-hide leaflet-
clickable" tabindex="0" style="margin-left: -17px; margin-top: -19px; left:
149px; top: 302px; z-index: 10304; transform: rotate(450deg);"></div>
【问题讨论】:
-
两者都应该工作,你试过像我提议的那样等待吗?可能有些类的加载时间比你预期的要晚一点
-
@drkthng driverChrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driverChrome.findElement(By.xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']")).click();没用
-
“没有工作”意味着它抛出了相同的错误消息,即找不到元素?你确定你的命令没有错别字吗?
-
@drkthng 它冻结了 30 秒并抛出 no such element: Unable to locate element
标签: java selenium testing selenium-webdriver selenium-chromedriver