- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果 Selenium2 在轮询有限时间后未能检索到 WebElement 对象,我有一个想法,即故障转移到 JavascriptExecutor。正如您所看到的,该方法有一个限制,即在调用 getElementByLocator 时需要预先定义“故障转移”Javascript 代码段。我想不出任何方法来动态地做到这一点。如果有人可以帮助我改进这一点,我将奖励最佳建议的答案,无论它有多小。
// failover example1: "document.getElementById('gbqfb')"
// failover example2: "document.querySelector("div#gbqfb")"
public static WebElement getElementByLocator(final By locator, String failover) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS);
.ignoring(NoSuchElementException.class,StaleElementReferenceException.class);
WebElement we = wait.until( ExpectedConditions
.presenceOfElementLocated( locator ) );
if ( we.isNull() ) {
JavascriptExecutor js = (JavascriptExecutor) driver;
if ( !failover.isEmpty() ) {
we = (WebElement)js.executeScript( failover );
if ( we.isNull() ) LOG.info("Still couldn't get element.");
} else {
LOG.info("No failover String available. Cannot try with " +
"a JavascriptExecutor.");
}
}
return we;
}
最佳答案
其实回答过类似的问题here
我不建议像这样将任何东西委托(delegate)给javascript..使用Selenium给你的东西..它已经足够了。
我在我构建的每个框架中都放入了一些东西,这是非常有效的。这是从框架中找到的摘录 here .
在对对象执行任何操作之前,我实现了一种伪等待类型的方法。自己尝试一下。效率非常高。
这些是来自 AutomationTest 的方法类
/**
* Checks if the element is present or not.<br>
* @param by
* @return <i>this method is not meant to be used fluently.</i><br><br.
* Returns <code>true</code> if the element is present. and <code>false</code> if it's not.
*/
public boolean isPresent(By by) {
if (driver.findElements(by).size() > 0) return true;
return false;
}
/**
* Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
*/
private WebElement waitForElement(By by) {
int attempts = 0;
int size = driver.findElements(by).size();
while (size == 0) {
size = driver.findElements(by).size();
if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
by.toString(),
MAX_ATTEMPTS));
attempts++;
try {
Thread.sleep(1000); // sleep for 1 second.
} catch (Exception x) {
fail("Failed due to an exception during Thread.sleep!");
x.printStackTrace();
}
}
if (size > 0) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");
return driver.findElement(by);
}
我所做的就是任何时候我执行某件事,例如
getText(By.cssSelector("input#someId"))
如果第一次没有找到,会等待1秒。如果找到则继续。随后执行 5 次,因此总共等待 5 秒..这是完全可以的,因为如果您没有找到所需的元素,那么您的测试实际上应该在此时失败。
此外,根据经验,我可以告诉您使用 driver.findElements()
比那些 WebDriverWait
更有效。
这并不意味着我不使用它们..只是不是为了这个。不幸的是,我没有将这个功能添加到 selenium 框架入门中,所以我只告诉你我何时使用 Webdriverwait 的。
所以我的测试看起来像 -
@Config(url="http://systemunder.test", browser=CHROME)
public class MyClass extends AutomationTest {
@Test
public void testSomething() {
setText(By.id("blah")) // if <* id="blah" /> doesn't exist, waits 1+ seconds for it to appear before interacting.
.click(By.id("Blah2")) // ^ same thing here.
.waitForPresent(By.cssSelector("ajaxy")); // this method right here would circumvent the hard waits, with webdriverwait's.
}
}
我不记得为什么它以前对我不起作用,但是在类似的事情中使用 webdriverwaits 是完美的。
关于java - 当 Selenium2 findElement 失败时故障转移到 JavascriptExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19286092/
在driver.findElements()中,我们获得了另一个用于查找size()的函数,该函数在driver.findElement()中不可用。 这是唯一的区别吗? 最佳答案 driver.fi
当我尝试使用 webDriver 搜索一个或多个元素时,它没有返回任何内容。操作系统:Ubuntu 14.04,框架:Intellij Idea,语言:Java。 我有这些声明: WebDriver
还是它们只影响 findElement?例如,如果我想测试页面上不存在的元素,我是要使用findElement(由于隐式等待会很慢),还是可以使用findElements(..).size() ==
我正在使用 WebElement.findElement(By.cssSelector('')).click(); 在页面上查找元素,但它返回了 “无法定位元素” ,但是当我使用 WebDriver.
当我执行下面的代码时,代码工作得很好 WebElement element = driver.findElement(By.xpath("String1")); element.findElement
我正在使用 Protractor(Angular JS 的 webdriver 包装器),尽管我可以无限期地将 findElement 链接到单个 WebElement 实例,但当我尝试时会收到错误使
为什么要使用@FindBy和driver.findElement()? @FindBy迫使我将所有变量都移到类级别(大多数变量只需要在方法级别时使用)。似乎唯一能买到我的东西是我可以调用PageFac
无法在 Google map 中找到元素。 ::::: :::::: :::::: 我尝试使用此方法,但没有成功选择具有“gm-
我正在 Selenium Webdriver 中编写代码。我必须点击 HTML 代码为 的按钮 我的代码就像下面一样简单 driver.findElement(By.id("aui_3_4_0_1_
MobileElement followButton = (MobileElement) driver.findElement(By.xpath("//android.widget.ListView[
我完全陷入困境,不知道为什么我会得到这样的输出。这是相关的片段。 WebElement section = driver.findElement(By.xpath("//div[contains(@c
我需要一些帮助来尝试从以下网站获取本圣经章节的每一节经文作为数据框中的一行字符串。 我正在努力寻找正确的元素/不知道如何将 findElements() 与浏览器中的检查元素结合使用。任何关于如何对其
我需要获取所有文章标题的列表。但由于某种原因,Selenium 返回了文章 WebElement 的同一实例 3 次。网页 HTML 如下所示:
列表中的第一个很容易,因为您可以使用查找元素。我找到该元素并需要从前面和后面的 div 中获取信息。对于列表中的“n”元素,用于移动/向后到其他关联 div 的 xPath 语法是什么? 我尝试了各种
我正在尝试在我的框架中创建一个显式等待方法来处理用户的输入。该方法应该处理所有类型的搜索:id、xpath 和 css。但是,当我尝试测试此方法时,错误返回一个奇怪的错误 显式等待方法 public
我正在尝试查找从 PCA Predict API 生成的元素(可在此链接中找到)。 http://www.pcapredict.com/en-gb/address-capture-software/
来自维基文档 https://github.com/SeleniumHQ/selenium/wiki/PageFactory我发现,如果脚本使用 eg. 找到了一些元素@FindBy(id = "q"
代码 1 resultsBoard.findElements(By.css(mySelector)).then(function(elements) { elements.forEach(func
我这里的代码行是: List element = driver.findElements(By.xpath("*")); for(int i=0; i element = driver.findEle
在使用 Selenium 网络测试时,有几种方法可以识别 WebElements。 根据我的经验,我使用过以下选择器: 类名 - By.className() CSS 选择器 - By.cssSele
我是一名优秀的程序员,十分优秀!