Problem with WebdriverWait in python webdriver

2,156 views
Skip to first unread message

koti

unread,
Sep 29, 2011, 1:23:17 AM9/29/11
to Selenium Users
Hi,

I am having problem in using the python version of the webdriver.

If the webpage has drop-down elements or hidden elements, my python
code not able to detect those elements. For this problem, I found some
solutions in the web as, increasing the implicit wait time or using
the WebDriverWait class. I have tried both, but i didn't find solution
to my problem.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from selenium.webdriver.support.ui import WebDriverWait

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

def test_bz(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("name").clear()
driver.find_element_by_id("name").send_keys("XXXXX")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("XXXXXXX")

driver.find_element_by_css_selector("input[type=image]").click()
try:
WebDriverWait(driver, 10).until(driver.find_element_by_id("Site
Statistics")).click()
#driver.find_element_by_link_text("Site Statistics").click()
driver.find_element_by_link_text("XXXXX").click()
driver.find_element_by_link_text("2011-09-25").click()
driver.find_element_by_css_selector("#signout > a").click()
finally:
driver.quit()

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


I got the following error after executing the above code,

Traceback (most recent call last):
File "bz.py", line 23, in test_bz
WebDriverWait(driver, 10).until(driver.find_element_by_id("Site
Statistics")).click()
File "/usr/lib/python2.7/site-packages/selenium-2.5.0-py2.7.egg/
selenium/webdriver/remote/webdriver.py", line 172, in
find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/usr/lib/python2.7/site-packages/selenium-2.5.0-py2.7.egg/
selenium/webdriver/remote/webdriver.py", line 525, in find_element
{'using': by, 'value': value})['value']
File "/usr/lib/python2.7/site-packages/selenium-2.5.0-py2.7.egg/
selenium/webdriver/remote/webdriver.py", line 144, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium-2.5.0-py2.7.egg/
selenium/webdriver/remote/errorhandler.py", line 118, in
check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: 'Unable to locate element:
{"method":"id","selector":"Site Statistics"}' ; Stacktrace: Method
WebDriverError threw an error in
file:///tmp/tmpFpiXUF/extensions/fxdr...@googlecode.com/resource/modules/utils.js

Please help me to get rid of this problem.

Thanks

Krishnan Mahadevan

unread,
Sep 29, 2011, 6:44:35 AM9/29/11
to seleniu...@googlegroups.com
AFAIK, webdriver can only work on html elements visible on the page. You seem to be trying to work with "hidden" elements.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Luke Inman-Semerau

unread,
Sep 29, 2011, 10:30:16 AM9/29/11
to seleniu...@googlegroups.com
That's not the proper way to do a webdriver wait in python, it's not actually waiting to call your find. The until method takes a function as an argument. You can do that in a couple ways, here's two:

def findSiteStats(driver):
return driver.find_element_by_id("Site Statistics")

WebDriverWait(driver, 10).until(findSiteStats).click()


Or using a lambda function:

WebDriverWait(driver, 10).until(lambda d : d.find_element_by_id("Site Statistics")).click()


The other question is, does your page really have a HTML element with id="Site Statistics" ?


-Luke

koti

unread,
Sep 29, 2011, 11:02:26 AM9/29/11
to Selenium Users
Hi Luke,
Thanks a lot. It was a silly mistake. As you said there was no element
with such an id . I should have used find_element_by_link_text
instead. (/me first timer). Sorry about that.

Thanks to Krishnan too

-
Koti

On Sep 29, 2:30 pm, Luke Inman-Semerau <luke.seme...@gmail.com> wrote:
> That's not the proper way to do a webdriver wait in python, it's not actually waiting to call your find. The until method takes a function as an argument. You can do that in a couple ways, here's two:
>
> def findSiteStats(driver):
>     return driver.find_element_by_id("Site Statistics")
>
> WebDriverWait(driver, 10).until(findSiteStats).click()
>
> Or using a lambda function:
>
> WebDriverWait(driver, 10).until(lambda d : d.find_element_by_id("Site Statistics")).click()
>
> The other question is, does your page really have a HTML element with id="Site Statistics" ?
>
> -Luke
>
> > file:///tmp/tmpFpiXUF/extensions/fxdri...@googlecode.com/resource/modules/utils.js
Reply all
Reply to author
Forward
0 new messages