【问题标题】:Selenium WebDriver (2.25) Timeout Not WorkingSelenium WebDriver (2.25) 超时不工作
【发布时间】:2012-10-14 06:58:17
【问题描述】:

我想我已经阅读了 Stack Overflow 上的所有 Selenium 超时问题,但在我的 Selenium webdriver 2.25(Python 2.7 绑定)中,隐式和显式超时都不起作用,并且“no_timeout_here =”行都将永远挂起 --

browser = webdriver.Firefox()
browser.implicitly_wait(6)               
browser.set_page_load_timeout(30)        
browser.get("http://www.google.com")
try:
    #no_timeout_here = browser.find_element_by_id("id_not_found")
    no_timeout_here = WebDriverWait(browser, 5).until(lambda browser:
            browser.find_element_by_id("id_not_found"))
except:
    raise

所有指针将不胜感激!

10 月 16 日更新

感谢 seleniumnewbie 的全面回答,但是,您的单元测试代码仍然挂在我的 Ubuntu 11.04(64 位)下 Python 2.7 --

(2012/10/17 11:51:58)$ time ./timeout.py 
^CTraceback (most recent call last):
...
KeyboardInterrupt

real    2m26.572s
user    0m0.368s
sys 0m0.232s

(2012/10/17 11:54:26)$ python -V
Python 2.7.2+

(2012/10/17 11:57:04)$ uname -a
Linux 3.0.0-26-generic #43-Ubuntu SMP Tue Sep 25 17:19:22 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

(2012/10/17 11:57:10)$ ls selenium-server-standalone-2.25.0.jar

我可以知道您的操作系统/Python 版本吗?

【问题讨论】:

  • 发生了什么?你在期待什么?你需要什么帮助?
  • “发生了什么?” -- "两条 no_timeout_here 行都永远挂起"; “你在期待什么?” --“no_timeout_here 行在合理的、最好是可配置的时间后引发异常”; “你需要什么帮助?” -- “一种实现超时的方法”。
  • 不确定这是否有帮助,但您的第一个“no_timeout_here”对我来说适用于 OS X、python 2.7.3 和 selenium 2.25.0

标签: python selenium selenium-webdriver qa


【解决方案1】:

如果您使用的是 Firefox 17 和 Selenium 2.26.0,那么您遇到了缺陷 #4814:http://code.google.com/p/selenium/issues/detail?id=4814

【讨论】:

【解决方案2】:

基于here找到的答案

1

我建议将 WebDriverWait 与 ExpectedConditons 一起使用。

//scroll down with Javascript first
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("selector")));
//interact with your element
element.click()

看看 Selenium 官方页面提供的指导: http://seleniumhq.org/docs/04_webdriver_advanced.html

2

特别尝试使用流利的等待。主要特点是:

Wait 接口的一个实现,它可以动态配置其超时和轮询间隔。 每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时的 NoSuchElementExceptions。

public WebElement fluentWait(final By locator){
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                        return driver.findElement(locator);
                }
                }
);
                           return  foo;              }     ;

所描述的方法返回您可以操作的 Web 元素。 所以方法如下: 1)您需要找到您希望在滚动后呈现的元素的选择器 例如

String cssSelector = "blablabla"

2) 使用 js 向下滚动 3)

WebElement neededElement  = fluentWait(cssSelector);
neededElement.click();
//neededElement.getText().trim();

你可以得到更多关于流利等待here的信息

更新


from selenium import webdriver
import unittest, time, re

class Sdsdsd(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []

    def test_sdsdsd(self):
        driver = self.driver
        driver.get ("http://www.google.com")
        try:
             driver.find_element_by_id("id_not_found")# I am only searching for the element not assigning it to anything.
        except:
            raise


    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

我得到了这个异常,这是期望的

E
======================================================================
ERROR: test_sdsdsd (__main__.Sdsdsd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "sdsdsd.py", line 19, in test_sdsdsd
    driver.find_element_by_id("id_not_found")
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 172, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 525, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 144, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/errorhandler.py", line 110, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"id_not_found"}' ; Stacktrace: Method WebDriverError threw an error in file:///private/var/folders/TA/TAS7MYfcEuG3lBNHwhrjRU+++TI/-Tmp-/tmpEf_lrD/extensions/fxdriver@googlecode.com/resource/modules/utils.js 

----------------------------------------------------------------------
Ran 1 test in 33.818s

FAILED (errors=1)

另请注意,它在 33 秒后失败,这意味着它在出错之前等待了 30 秒。

当我将隐式等待更改为self.driver.implicitly_wait(15)

我收到此错误

E
======================================================================
ERROR: test_sdsdsd (__main__.Sdsdsd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "sdsdsd.py", line 19, in test_sdsdsd
    driver.find_element_by_id("id_not_found")
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 172, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 525, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/webdriver.py", line 144, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.6/site-packages/selenium-2.2.0-py2.6.egg/selenium/webdriver/remote/errorhandler.py", line 110, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"id_not_found"}' ; Stacktrace: Method WebDriverError threw an error in file:///private/var/folders/TA/TAS7MYfcEuG3lBNHwhrjRU+++TI/-Tmp-/tmpXSbCY0/extensions/fxdriver@googlecode.com/resource/modules/utils.js 

----------------------------------------------------------------------
Ran 1 test in 18.843s

FAILED (errors=1)

【讨论】:

  • 1 只是我的(失败的)Python 示例的 Java 绑定
  • 谢谢,但我还不相信我必须将整个 Python 程序重写为 Java。
  • 你可以简单地分享这个链接stackoverflow.com/questions/12692172/… ;-)
  • @Jerry,我设法收到了错误消息,请查看我的更新。
  • @seleniumnewbie,谢谢,但你的测试代码仍然挂在我的机器上。我已经用详细信息更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2016-09-16
相关资源
最近更新 更多