Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Web驱动程序中检测元素的不一致性_Ruby_Testing_Automation_Selenium Webdriver - Fatal编程技术网

Ruby Web驱动程序中检测元素的不一致性

Ruby Web驱动程序中检测元素的不一致性,ruby,testing,automation,selenium-webdriver,Ruby,Testing,Automation,Selenium Webdriver,我正在尝试运行自动化测试脚本(SeleniumWebDriver2+ruby),但最近遇到了一个奇怪的问题。 到昨天为止运行得非常好的脚本现在抛出了“没有这样的元素异常”。 然而,当检入firebug时,该路径肯定存在,并且应用程序中没有任何变化。 脚本无法检测以下代码中的iframe2:- browser.manage.timeouts.implicit_wait=20秒 我在谷歌上搜索过这种前后矛盾的行为,但找不到任何合理的解决办法。其中一篇文章可以追溯到2009年,指责不稳定的Seleni

我正在尝试运行自动化测试脚本(SeleniumWebDriver2+ruby),但最近遇到了一个奇怪的问题。 到昨天为止运行得非常好的脚本现在抛出了“没有这样的元素异常”。 然而,当检入firebug时,该路径肯定存在,并且应用程序中没有任何变化。 脚本无法检测以下代码中的iframe2:-

browser.manage.timeouts.implicit_wait=20秒

我在谷歌上搜索过这种前后矛盾的行为,但找不到任何合理的解决办法。其中一篇文章可以追溯到2009年,指责不稳定的SeleniumWebDriver

还有其他人也经历过吗?有什么变通方法/解决方案吗

帮帮伙计们

谢谢


Abhishek

这不太可能是Webdriver不稳定的问题,但执行时间与以前不同。我建议您考虑一下如何使用一些您很难找到的元素。您可以在selenium文档中阅读有关它们的内容

这是seleniumhq示例:

require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://somedomain/url_that_delays_loading"

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
  element = wait.until { driver.find_element(:id => "some-dynamic-element") }
ensure
  driver.quit
end

我也面临同样的问题。DOM释放了对相关元素的引用。它可以是StaleStateReferenceExceptionNoTouchElementException。处理这种情况有两种方法。 (虽然我的解决方案是Java,但基本概念是一样的。)

通过使用以下方法,可以尝试单击图元。如果引发异常,则捕获异常并再次尝试单击,直到元素出现:

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            Actions action = new Actions(driver);
            WebElement userClick = wait.until(ExpectedConditions.presenceOfElementLocated(by));
            action.moveToElement(userClick).click().build().perform();
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
        }
        catch(NoSuchElementException e) {
            System.out.println("No Such Element Found");
        }
        attempts++;
    }

    return result;
}
public boolean retryingFindClick(By){
布尔结果=假;
int=0;
而(尝试次数<2次){
试一试{
动作动作=新动作(驱动);
WebElement userClick=wait.until(ExpectedConditions.presenceOfElementLocated(by));
action.moveToElement(userClick).click().build().perform();
driver.findElement(by).click();
结果=真;
打破
}捕获(StaleElementReferenceException e){
System.out.println(“StaleElementReferenceException”);
}
捕获(无接触元素例外e){
System.out.println(“未找到此类元素”);
}
尝试++;
}
返回结果;
}

谢谢,Dan,我会查出来的。如果你能加入一些测试代码,你会得到更具体的答案。@DanSnell添加了代码。。
public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            Actions action = new Actions(driver);
            WebElement userClick = wait.until(ExpectedConditions.presenceOfElementLocated(by));
            action.moveToElement(userClick).click().build().perform();
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
        }
        catch(NoSuchElementException e) {
            System.out.println("No Such Element Found");
        }
        attempts++;
    }

    return result;
}