问题
I think I've read all the Selenium timeout questions on Stack Overflow, yet neither implicit nor explicit timeout works in my Selenium webdriver 2.25 (Python 2.7 binding) and both "no_timeout_here =" lines would hang forever --
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
All pointers will be greatly appreciated!
Update Oct. 16th
Thanks to seleniumnewbie for your comprehensive answer, however, your unittest code still hangs on my Ubuntu 11.04 (64-bit) under 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
May I know your OS/Python version?
回答1:
If you are using Firefox 17 and Selenium 2.26.0 then you are hitting defect #4814: http://code.google.com/p/selenium/issues/detail?id=4814
回答2:
Based on answers found here
1
I would recommend using WebDriverWait with 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()
Take a look at the guidance provided by Selenium Official page: http://seleniumhq.org/docs/04_webdriver_advanced.html
2
try using fluent wait in particular. The main feature is:
An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
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; } ;
The method described returns you web element you can operate with. So the approach be the following: 1) you need to find the selectors of elements you expect to be rendered after scrolling e.g.
String cssSelector = "blablabla"
2) scroll down with js 3)
WebElement neededElement = fluentWait(cssSelector);
neededElement.click();
//neededElement.getText().trim();
you can get more info about fluent wait here
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()
and I get this exception, which is desired
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)
Also note that it failed after 33 seconds, which means it waited for 30 seconds before erroring out.
When i change the implicit wait to self.driver.implicitly_wait(15)
I get this error
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)
来源:https://stackoverflow.com/questions/12880024/selenium-webdriver-2-25-timeout-not-working