0
這是我的python腳本的代碼片段。我使用PhantomJS 1.9.1和Python 2.7。Python中的Selenium PhantomJS未在ec2實例上運行
def setUp(self):
self.driver = webdriver.PhantomJS()
self.driver.implicitly_wait(30)
self.base_url = "http://***"
self.verificationErrors = []
self.accept_next_alert = True
def test_one(self):
driver = self.driver
driver.get(self.base_url + "/"+datestring)
driver.execute_script('document.querySelector("span.zoom_link").click();')
targeturl = anchor_tag.get_attribute("src")
datestring =datestring.replace('/','_')
urllib.urlretrieve(targeturl, datestring+".jpg")
代碼運行按預期在我的MAC OS本地,但是當我部署在EC2上百分之OS實例相同的腳本,它拋出以下錯誤
Traceback (most recent call last):
File "one.py", line 28, in test_one
driver.execute_script('document.querySelector("span.zoom_link").click();')
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 461, in execute_script
{'script': script, 'args':converted_args})['value']
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Error Message => ''undefined' is not a function (evaluating 'document.querySelector("span.zoom_link").click()')'
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"130","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:39740","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"fds\", \"args\": [], \"script\": \"document.querySelector(\\\"span.zoom_link\\\").click();\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/c1c222d0-1cb6-11e6-bd87-e5a4b311ba07/execute"}
您是否在本地和遠程機器上使用相同版本的'Selenium'和'PhantomJS'? – dm295
它是我的錯誤,PhantomJS版本在我的ec2中是1.9.1,當我將它改爲2.1.1時,它的工作。謝謝 – nithin