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

时间:2017-08-31 15:20:03

标签: python selenium web-scraping phantomjs

我正在尝试填写these page的形式,我检查了HTML代码和我正在寻找的表单,但是当我运行我的代码时,我收到了一个我从未见过的错误硒。

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

我想填写这些表格。

<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中使用相同的代码(更改网址和元素名称)时,它可以正常工作。

谢谢大家。

1 个答案:

答案 0 :(得分:1)

这是PhantomJS的问题/错误。

我建议您使用Chrome作为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,但我真的怀疑你会得到很多帮助