检测元素Selenium Webdriver的不一致性

我正在尝试运行自动化测试脚本(Selenium Webdriver2 + ruby​​),但最近遇到了一个奇怪的问题。 直到昨天完美无缺的脚本现在正在抛出“没有这样的元素exception”。 但是,当在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) 

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

还有其他人经历过这个吗? 任何变通方法/解决方案?

帮帮忙!

谢谢。

阿布舍克

这不太可能成为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丢失了对相关元素的引用。 它可以是StaleStateReferenceExceptionNoSuchElementException 。 有两种方法可以处理这种情况。 (虽然我的解决方案是Java。基本概念是相同的。)

通过使用以下方法,您可以尝试单击元素。 如果抛出exception,则捕获exception并尝试再次单击,直到元素存在:

 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; }