I'm trying out Selenium. I've built a quick script using the Selenium IDE. It logs in to a page, opens up a 'Search' window, does a search, and then opens up one of the results.
When I run the test in the Selenium IDE, it works fine. When I export to a Java JUnit 4 test case, it works fine. But when I export to a Python 2 unittest, the xpath searching seems to be very buggy...
Example:
The page has an input element with name='keyword'. When I search using java with driver.findElement(By.name("keyword"));
it's all good - it finds the element.
When I search using python with browser.find_element_by_name("keyword")
, it can't seem to find it. I've also tried browser.find_element_by_xpath("//input[@name='keyword']")
and browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input")
, the second of which is the xpath I get when I use firebug and the Copy XPath
feature.
I've tried adding a delay to the python code before it searches for the input, but to no avail.
Does anyone have any idea why the python calls can't seem to find the element? This seems to happen with different elements throughout the pages of the web app (but doesn't happen in the Selenium IDE that I've found so far)...
I appreciate any help you guys can provide!
Edit:
The error showing in the console is:
Traceback (most recent call last):
File "test.py", line 37, in <module>
elem = browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 213, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 671, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"/html/body/form/div/ul/li[2]/input"}'
Cheers
Jarrett