【问题标题】:Getting Unusual Error while using Selenium and PhantomJS in Python在 Python 中使用 Selenium 和 PhantomJS 时出现异常错误
【发布时间】:2017-08-31 15:20:03
【问题描述】:

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

我在带有 Selenium 的 Python 3.6.1 中使用 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 和元素名称)时,它工作正常。

谢谢大家。

【问题讨论】:

  • 在 Windows 上与 Chrome 一起工作正常。
  • 关于为什么这在 phantomJS 上不起作用的任何想法?我的网络受到限制,无法下载 Chrome 的网络驱动程序
  • 不知道,如果你有驱动,试试Firefox
  • 我也没有。它必须与 phantomJS 一起使用):
  • 尝试通过xpath选择元素。看看它是否有效。

标签: python selenium web-scraping phantomjs


【解决方案1】:

这是 PhantomJS 的问题/错误。

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

【讨论】:

  • 明白。有没有可能将chrome用作无头浏览器?
  • 是的,您可以通过创建 ChromeOptions 来使用它。也只有在它有效的情况下才使用无头,我已经看到其他与无头的问题,它与 chrome 不同,一些网站无法正常工作。因此,请检查您的是否有效,如果有效,请使用它。
  • @ThalesMarques 阅读此intoli.com/blog/running-selenium-with-headless-chrome。从我听到的关于新无头的消息来看,你不能使用扩展。一种更简单的方法是使用 chrome 选项,但如果你有 linux,最好通过最新的 Chrome 无头运行它而不是 chrome 选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多