【问题标题】:selenium.common.exceptions.WebDriverException was unhandled by user code and HTTP Error 400. The request hostname is invalid with Seleniumselenium.common.exceptions.WebDriverException 未由用户代码和 HTTP 错误 400 处理。请求主机名对 Selenium 无效
【发布时间】:2018-12-21 10:55:48
【问题描述】:

最近我正在尝试学习如何在 Python 中使用 Selenium,但是当我尝试初始化 Internet Explorer 时,我得到了一个 WebDriver 异常。引发异常的行是这一行(几乎是代码的开头):

driver = webdriver.Ie(r'pathtothefile\MicrosoftWebDriver.exe')

(我放了路径是因为我不知道让这个参数变得不必要的PATH在哪里哈哈)

错误如下:

selenium.common.exceptions.WebDriverException was unhandled by user code
Message: Message: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>

当我在公司网络中执行此操作时,我猜这可能与某些限制有关……?或者也许是因为我使用的是公司计算机,所以他们可以采取一些特定的行动......?无论如何,无论如何,我该怎么做才能解决这个问题?

提前致谢!

【问题讨论】:

    标签: python-3.x selenium internet-explorer selenium-webdriver selenium-iedriver


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.WebDriverException was unhandled by user code
    .
    <HTML><HEAD><TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
    <BODY><h2>Bad Request - Invalid Hostname</h2>
    <hr><p>HTTP Error 400. The request hostname is invalid.</p>
    

    ...表示您的程序无法启动/产生新的 WebBrowsing 会话,即 InternetExplorer Browser 会话。

    您的主要问题是您使用的二进制文件类型之间的不兼容。

    要初始化 Internet Explorer 浏览器,您需要使用 IEDriverServer 可执行文件,而不是 MicrosoftWebDriver.exe(用于 Edge 浏览器)。


    解决方案

    您需要从selenium-release.storage.googleapis.com下载并解压匹配的IEDriverServer,并传递IEDriverServer的绝对路径如下:

    • Windows 操作系统示例:

      driver = webdriver.Ie(executable_path=r'C:\path\to\IEDriverServer.exe')
      
    • Linux OS 和 MAC OS X 示例:

      driver = webdriver.Ie(executable_path='/path/to/IEDriverServer')
      

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 2019-11-30
      • 2015-08-17
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多