Selenium in Python

匿名 (未验证) 提交于 2019-12-03 02:48:02

问题:

I've been using urllib2 to access webpages, but it doesn't support javascript, so I took a look at Selenium, but I'm quite confused even having read its docs.

I downloaded Selenium IDE add-on for firefox and I tried some simple things.

from selenium import selenium import unittest, time, re  class test(unittest.TestCase):     def setUp(self):         self.verificationErrors = []         self.selenium = selenium("localhost", 4444, "*chrome", "http://www.wikipedia.org/")         self.selenium.start()      def test_test(self):         sel = self.selenium         sel.open("/")         sel.type("searchInput", "pacific ocean")         sel.click("go")         sel.wait_for_page_to_load("30000")      def tearDown(self):         self.selenium.stop()         self.assertEqual([], self.verificationErrors)  if __name__ == "__main__":     unittest.main() 

I just access wikipedia.org and type pacific ocean in the search field, but when I try to compile it, it gives me a lot of errors.

回答1:

If running the script results in a [Errno 111] Connection refused error such as this:

% test.py E ====================================================================== ERROR: test_test (__main__.test) ---------------------------------------------------------------------- Traceback (most recent call last):   File "/home/unutbu/pybin/test.py", line 11, in setUp     self.selenium.start()   File "/data1/unutbu/pybin/selenium.py", line 189, in start     result = self.get_string("getNewBrowserSession", [self.browserStartCommand, self.browserURL, self.extensionJs])   File "/data1/unutbu/pybin/selenium.py", line 219, in get_string     result = self.do_command(verb, args)   File "/data1/unutbu/pybin/selenium.py", line 207, in do_command     conn.request("POST", "/selenium-server/driver/", body, headers)   File "/usr/lib/python2.6/httplib.py", line 898, in request     self._send_request(method, url, body, headers)   File "/usr/lib/python2.6/httplib.py", line 935, in _send_request     self.endheaders()   File "/usr/lib/python2.6/httplib.py", line 892, in endheaders     self._send_output()   File "/usr/lib/python2.6/httplib.py", line 764, in _send_output     self.send(msg)   File "/usr/lib/python2.6/httplib.py", line 723, in send     self.connect()   File "/usr/lib/python2.6/httplib.py", line 704, in connect     self.timeout)   File "/usr/lib/python2.6/socket.py", line 514, in create_connection     raise error, msg error: [Errno 111] Connection refused  ---------------------------------------------------------------------- Ran 1 test in 0.063s  FAILED (errors=1) 

then the solution is most likely that you need get the selenium server running first.

In the download for SeleniumRC you will find a file called selenium-server.jar (as of a few months ago, that file was located at SeleniumRC/selenium-server-1.0.3/selenium-server.jar).

On Linux, you could run the selenium server in the background with the command

java -jar /path/to/selenium-server.jar 2>/dev/null 1>&2 & 

You will find more complete instructions on how to set up the server here.



回答2:

I would suggest you to use a webdriver, you can find it here: http://code.google.com/p/selenium/downloads/list. If you want to write tests as a coder (and not with the use of your mouse), that thing would work better then the RC version you're trying to use, at least because it would not ask you for an SeleniumRC Jar Instance. You would simply have a binary of a browser or use those ones that are already installed on your system, for example, Firefox.



回答3:

I faced with this issue in my project and found that problem was in few webdriver.get calls with very small time interval between them. My fix was not to put delay, just remove unneeded calls and error disappears. Hope, it can helps for somebody.



文章来源: Selenium in Python
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!