硒的webdriver(2.25)超时不工作(Selenium WebDriver (2.25) T

2019-09-24 02:25发布

我想,我读过所有关于堆栈溢出的硒超时的问题,但无论隐式还是显超时在我的作品硒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

我可以知道你的OS / Python版本?

Answer 1:

如果您使用的是Firefox 17和硒2.26.0然后你打的缺陷#4814: http://code.google.com/p/selenium/issues/detail?id=4814



Answer 2:

基于找到答案在这里

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()

看看硒官方网页所提供的指导: http://seleniumhq.org/docs/04_webdriver_advanced.html

2

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

可具有在运行配置其超时和轮询间隔等待接口的实现。 每个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;              }     ;

该方法描述返回你可以用您操作网页元素。 因此,该方法是以下几点:1)你需要找到的元素,你想到滚动如后要呈现的选择

String cssSelector = "blablabla"

2)与JS 3向下滚动)

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

你可以得到有关流畅等待更多信息点击这里

UPDATE


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)


文章来源: Selenium WebDriver (2.25) Timeout Not Working