我将 Selenium for Python 与 PhantomJS Ghost 驱动程序一起使用,以便单击 href 中包含 Javascript 的链接,例如来自this https://structuredginniemaes.ginnienet.com/Download/关联。我可以很好地获取链接作为一个元素,但是当我尝试单击它时,事情就会出错。这是一些代码(注意 - url 是一个列表,其中第一个元素是 Jscript 链接,第二个元素是链接文本)
def get_jscript_down(self,url):
driver = webdriver.PhantomJS()
print(self.exact_url)
driver.get(self.exact_url)
elements = driver.find_elements_by_tag_name("a")
for ele in elements:
if ele.text == url[1].encode("utf-8").replace(" "," "):
break
ele.click()
print("Cannot yet download %s" % url)
return False
除了 ele.click() 之外,一切都按我需要的方式工作。当我执行该命令时,我得到一个难看的 WebDriverException。
selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click failed: ReferenceError: Can\'t find variable: __doPostBack\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:34441","User-Agent":"Python-urllib/2.6"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"71c20b50-ca21-11e2-a03c-f58c49e5a1bc\", \"id\": \":wdc:1370025577147\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/71c20b50-ca21-11e2-a03c-f58c49e5a1bc/element/%3Awdc%3A1370025577147/click"}' ; Screenshot: available via screen
我不太确定为什么这次点击失败。据我所读,我应该可以以这种方式使用 click 。下面是堆栈跟踪的相关部分(从事情开始崩溃之前到结束)
line 283, in get_jscript_down
ele.click()
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 54, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 228, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
任何想法将不胜感激。我很困惑。