【问题标题】:Inconsistency in detecting elements Selenium Webdriver检测元素不一致 Selenium Webdriver
【发布时间】:2014-05-14 01:39:12
【问题描述】:

我正在尝试运行自动化测试脚本(Selenium Webdriver2 + ruby​​),但后来遇到了一个奇怪的问题。 直到昨天还运行良好的脚本现在抛出“没有这样的元素异常”。 但是,当在 firebug 中检查时,该路径肯定存在,并且应用程序中没有任何变化。 该脚本未能在以下代码中检测到 iframe2:-

browser.manage.timeouts.implicit_wait = 20 # 秒

                                                        ############ GO TO OVERVIEW TAB ################

#Adding wait until quote is created and page is ready for content tab click.
wait = Selenium::WebDriver::Wait.new(:timeout => 5)
wait.until { browser.find_element(:id => "j_id0:tabDetailedContent_lbl") }

browser.find_element(:id => "j_id0:tabDetailedContent_lbl").click



iframe = browser.find_element(:id =>'CPQFrame')
browser.manage.timeouts.implicit_wait = 10
browser.switch_to.frame(iframe) 

browser.find_element(:css,".processBarElement.noSelected").click


#frame.browser.find_element(:css,".processBarElement.noSelected").click

#browser.manage.timeouts.implicit_wait = 30 # seconds

iframe2 = browser.find_element(:xpath,'html/body/div[3]/div[2]/div[2]/div[3]/iframe')

#browser.manage.timeouts.implicit_wait = 10
browser.switch_to.frame(iframe2)

我搜索了这种不一致的行为,但找不到任何合理的解决方案。一篇可以追溯到 2009 年的帖子归咎于不稳定的 Selenium Webdriver。

有没有其他人也经历过这种情况?任何解决方法/解决方案?

帮助大家!

谢谢。

阿布舍克

【问题讨论】:

  • 如果您可以包含一些测试代码,您将获得更具体的答案。
  • @DanSnell 添加了代码..

标签: ruby testing automation selenium-webdriver


【解决方案1】:

我也遇到过同样的问题。 DOM 失去了对相关元素的引用。它可以是 StaleStateReferenceExceptionNoSuchElementException。有两种方法可以处理这种情况。 (虽然我的解决方案是在 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;
}

【讨论】:

    【解决方案2】:

    这不太可能是 Webdriver 不稳定但执行时间表现与以前不同的问题。对于您很难找到的一些元素,我建议您使用explicit waits。您可以在 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
    

    【讨论】:

    • 谢谢丹,会检查一下。
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2018-03-14
    相关资源
    最近更新 更多