Reputation: 11
I'm new to selenium python. right now i'm trying to run python code that already converted from selenium Ide. when i'm ran the code i got this error. i'm using eclipse to run the code
ERROR: test_isnin2 (__main__.Isnin2)
======================================================================
ERROR: test_isnin2 (__main__.Isnin2)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\EclipseWorkspaces\csse120\python\src\Isnin2.py", line 20, in test_isnin2
driver.get("/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 154, in get
self.execute(Command.GET, {'url': url})
File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 144, in execute
self.error_handler.check_response(response)
File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 111, in check_response
zeroeth = value['stackTrace'][0]
IndexError: list index out of range
----------------------------------------------------------------------
Ran 1 test in 35.406s
FAILED (errors=1)
my coding as below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Isnin2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.google.com.my/"
self.verificationErrors = []
def test_isnin2(self):
driver = self.driver
driver.get("/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
driver.find_element_by_css_selector("em").click()
driver.find_element_by_id("lst-ib").click()
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("selenium python")
driver.find_element_by_link_text("Setting Up Selenium with Python").click()
driver.find_element_by_link_text("selenium.py").click()
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'm really hope someone can help me since i'm new to selenium python.
Upvotes: 1
Views: 2423
Reputation: 6356
I'd comment on Constantinius answer if I could, but I don't have the rep . . .
I used this question as an excuse to look into the IDE - looks like the code above is pretty much a straight export to Python Webdriver, and in my test case, it results in the same malformed .get (where the base_url is not provided).
The rest of the issues Constantinius mentions are a result of the IDE's limited abilities. The is_element_present method looks like it's part of a work in process in the IDE, and I'm guessing it's a placeholder for actual checks.
Upvotes: 1
Reputation: 35069
It seems like you do not supply your base_url
to the driver
. Maybe you should replace this line:
driver.get("/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
with something like this:
driver.get(self.base_url + "/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
EDIT:
Also: do you want to test something with the is_element_present
method? With the Python unittest framework the method name has to start with test
. So if you want it as a test rename it to test_is_element_present
.
Additionally: If you plan multiple tests on a response, you should place the driver.get()
call in the setUp
method, because the order of the test does not necessarily have to be the same as you have written the tests. setUp
on the other hand is guaranteed to be run before any tests are called.
Upvotes: 1