在 Python 中使用 Selenium 和 PhantomJS 时出现异常错误

Getting Unusual Error while using Selenium and PhantomJS in Python

我正在尝试填写 these page 的表格,我检查了 HTML 代码,我正在寻找的表格在那里,但是当我 运行 我的代码时,我得到了一个我在 selenium 中从未见过的错误。

我在 Python 3.6.1 和 Selenium 中使用 PhantomJS 2.1.1。

我要填写这些表格。

<input type="text" name="Dt_Ref" value="31/08/2017" maxlength="10" size="10" class="form_data" onBlur="formatCamp(this, 'date');javascript:if(REUNE.Dt_Ref.value!=REUNE.Dt_RefAnt.value){REUNE.target='_self';REUNE.action='reune.asp';REUNE.submit()};" onKeyPress="return(validaConteudo(event, this, 'date'))" onKeyUp="saltaCampo(event, this, 'date',8)" onFocus="removeCaracs(this, 'date')">  <a style="cursor:hand; cursor:pointer;" onclick="openCalendar(document.REUNE.elements['Dt_Ref']);return false;">

我的密码是:

from selenium import webdriver


url = 'http://www.anbima.com.br/reune/reune.asp'
path_phantom = 'C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\Lib\site-packages\phantomjs-2.1.1-windows\bin\phantomjs.exe'

#Processing

driver = webdriver.PhantomJS(executable_path= path_phantom)
driver.get(url)
name = driver.find_element_by_name("Dt_Ref")
driver.quit()

我得到了这个错误:

Traceback (most recent call last):
  File "C:\Users\TBMEPYG\Desktop\beta_anbima.py", line 11, in <module>
    name = driver.find_element_by_name("Dt_Ref")
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: {"request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:64945","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"name\", \"value\": \"Dt_Ref\", \"sessionId\": \"cc170460-8e5e-11e7-ad22-8d3d940cf70e\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/cc170460-8e5e-11e7-ad22-8d3d940cf70e/element"}}
Screenshot: available via screen

这是我的代码的第一步。我在银行代理下,这可能是问题所在。当我从 Chrome 访问时,页面工作正常。 当我尝试在 this site 中使用相同的代码(更改 url 和元素名称)时,它工作正常。

谢谢大家

这是一个带有 PhantomJS 的 issue/bug。

我建议您使用 Chrome 本身或 docker 图片用于 selenium/standalone-chrome

如果您查看 https://github.com/ariya/phantomjs/issues,有 1923 个未解决的问题,这不言自明。

你甚至不能查询

driver.find_elements_by_xpath("//*")

你会得到错误

NoSuchElementException: Message: {"request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"87","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:53425","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//*\", \"sessionId\": \"eb09e680-8e72-11e7-8b72-d1137b47fa59\"}","url":"/elements","urlParsed":{"anchor":"","query":"","file":"elements","directory":"/","path":"/elements","relative":"/elements","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/elements","queryKey":{},"chunks":["elements"]},"urlOriginal":"/session/eb09e680-8e72-11e7-8b72-d1137b47fa59/elements"}}
Screenshot: available via screen

所以打开一个错误并计算 1924,但我真的怀疑你会得到多少帮助