简体   繁体   English

Python中的Selenium PhantomJS无法在ec2实例上运行

[英]Selenium PhantomJS in Python not running on ec2 instance

This is the code snippet of my python script. 这是我的python脚本的代码片段。 I am using PhantomJS 1.9.1 and python 2.7. 我正在使用PhantomJS 1.9.1和python 2.7。

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

The code is running as expected in my mac os locally, but when I deploy the same script on ec2 cent os instance, its throwing the following error 该代码在本地Mac OS中按预期运行,但是当我在ec2 cent 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"}

Try using an attribute selector , ie change the querySelector from 尝试使用属性选择器 ,即从以下位置更改querySelector

driver.execute_script('document.querySelector("span.zoom_link").click();')

to

driver.execute_script('document.querySelector(\'span[class="zoom_link"]\').click();')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM