无法在python中使用selenium打开IE

2024-04-27

我正在 Windows 10 计算机、Internet Explorer 11、python 3.6、selenium 3.4.3 和 IEDriverServer 3.5 上运行。我正在尝试使用以下代码打开 IE。

from selenium import webdriver
import os


driverLocation = "C:\\Users\\JD\\PycharmProjects\\Lib\\IEDriverServer.exe"
os.environ["webdriver.ie.driver"] = driverLocation
driver = webdriver.Ie(driverLocation)
google = "https://google.com"
driver.get(google)

输出:

Traceback (most recent call last):
  File "C:/Users/J/PycharmProjects/Automation/IE_Test.py", line 7, in <module>
    driver = webdriver.Ie(driverLocation)
  File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\ie\webdriver.py", line 57, in __init__
    desired_capabilities=capabilities)
  File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Invalid capabilities in alwaysMatch: unknown capability named platform

任何帮助将不胜感激,谢谢。

更新: 我将其添加到之前的代码中,

capabilities = DesiredCapabilities.INTERNETEXPLORER
print(capabilities["platform"])
print(capabilities["browserName"])

OUTPUT:

WINDOWS
internet explorer
 File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Invalid capabilities in alwaysMatch: unknown capability named platform

更新: 我也尝试过设置功能,但仍然收到相同的错误:“未知功能命名平台

caps = DesiredCapabilities.INTERNETEXPLORER.copy()
caps["platform"] = "WINDOWS"
caps["browserName"] = "internet explorer"
caps["requireWindowFocus"] = True
browser = webdriver.Ie(capabilities=caps,
                       executable_path="C:\\Users\\JD\\PycharmProjects\\Lib\\IEDriverServer.exe")
browser.get("https://www.facebook.com/")

我有同样的问题几天了。 我的解决方法是删除platform and version钥匙来自capabilities字典

Example:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

#create capabilities
capabilities = DesiredCapabilities.INTERNETEXPLORER

#delete platform and version keys
capabilities.pop("platform", None)
capabilities.pop("version", None)

#start an instance of IE
driver = webdriver.Ie(executable_path="C:\\your\\path\\to\\IEDriverServer.exe", capabilities=capabilities)

driver.get("https://www.google.com/")

到目前为止,我的猜测是,发生此错误是因为 w3c_caps 被作为唯一正确的功能传递。您可以在回溯中看到:

response = self.execute(Command.NEW_SESSION, parameters)

当你点击它时,你会看到:

w3c_caps["alwaysMatch"].update(capabilities) 

如你看到的here https://seleniumhq.github.io/selenium/docs/api/py/_modules/selenium/webdriver/remote/webdriver.html_W3C_CAPABILITY_NAMES 保存的值与我们传递的值不同。 我们将“WINDOWS”作为“平台”传递,而 _W3C_CAPABILITY_NAMES 具有“platformName”并且仅接受小型大写字母。 “版本”键也是如此。

因此我们添加了未被识别的功能。

这个解决方法绝不是完美的,我能够在 selenium java 中启动 IE,而无需删除一些功能。

EDIT:可以找到另一个解决方案here https://github.com/SeleniumHQ/selenium/issues/3808在 Grimlek 评论中,本质上是说你应该删除"capabilities": w3c_caps from start_session(self, capabilities, browser_profile=None)(来自remote\webdriver.py)。代码如下所示:

w3c_caps["alwaysMatch"].update(capabilities)
parameters = {"capabilities": w3c_caps,
                      "desiredCapabilities": capabilities}

那么您就不需要从功能中删除密钥。

另一个编辑:我刚刚将我的 selenium-python 从 3.4.3 更新到 3.5.0,不再需要搞乱功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在python中使用selenium打开IE 的相关文章

  • 从两个字典创建一个新列表

    这是一个关于Python的问题 我有以下字典列表 listA t 1 tid 2 gtm 3 c1 4 id 111 t 3 tid 4 gtm 3 c1 4 c2 5 id 222 t 1 tid 2 gtm 3 c1 4 c2 5 id
  • 使用 theano 进行多处理

    我正在尝试将 theano 与 cpu 多处理和神经网络库 Keras 结合使用 I use device gpu标记并加载 keras 模型 然后 为了提取超过一百万张图像的特征 我使用多处理池 该函数看起来像这样 from keras
  • 解析器生成

    我正在做一个项目软件抄袭检测 我打算用C语言来做这件事 因为我应该创建一个令牌生成器和一个解析器 但我不知道从哪里开始 任何人都可以帮助我解决这个问题 我创建了一个令牌数据库 并将令牌与我的程序分开 接下来我想做的就是比较两个程序以查明它是
  • 在 Django 中上传文件

    我在 Django 1 6 版本 中上传文件时遇到问题 当我尝试做的时候new file data save 在我的views py 中我收到此错误 quiz patent 22 medical record 2 exams 处的属性错误
  • Python Subversion 包装器库

    在颠覆的文档 http svnbook red bean com en 1 7 svn developer usingapi html svn developer usingapi otherlangs有一个从 Python 使用 Subv
  • set() 可以在 Python 进程之间共享吗?

    我正在 Python 2 7 中使用多重处理来处理非常大的数据集 当每个进程运行时 它会将整数添加到共享的 mp Manager Queue 中 但前提是其他进程尚未添加相同的整数 由于您无法对队列进行 in 式成员资格测试 因此我这样做的
  • 使用 gin 索引和 sqlalchemy 返回排名搜索结果

    我为全文搜索设置了 GIN 索引 我想获取与搜索查询匹配的记录列表 按排名排序 记录与搜索查询的匹配程度 对于结果 我只需要记录及其列 不需要用于排序的实际排名值 我有以下查询 它运行良好并从我的 postgresql 数据库返回预期结果
  • 从 python 的单词列表中查找最长的常见单词序列

    我搜索了很多解决方案 确实发现了类似的问题 这个答案 https stackoverflow com questions 21930757 longest repeated substring返回可能不属于输入列表中所有字符串的最长字符序列
  • 为什么 PySpark 中的 agg() 一次只能汇总 DataFrame 的一列? [复制]

    这个问题在这里已经有答案了 对于下面的数据框 df spark createDataFrame data Alice 4 300 Bob 7 677 schema name High 当我尝试找到最小值和最大值时 我只得到输出中的最小值 d
  • 为线条指定颜色

    我试图在 matplotlib 中绘制可变数量的行 其中 X Y 数据和颜色存储在 numpy 数组中 如下所示 有没有办法将颜色数组传递到绘图函数中 这样我就不必采取额外的步骤来单独为每条线分配颜色 我是否应该将 RGB 颜色数组转换为另
  • 来自异常导入 PendingDeprecationWarning ModuleNotFoundError:没有名为“异常”的模块

    我正在尝试使用Python 创建一个word 文档 我在终端中 pip install python docx 我的代码如下所示 from docx import Document document Document document sa
  • 使用 Python gdata 和 oAuth 2 对日历进行身份验证

    我正在将一个 Python 应用程序从 oAuth 1 迁移到 oAuth 2 该应用程序读取用户的 Google 日历提要 使用 oAuth 1 如果用户可以使用他的 GMail 进行身份验证 我的应用程序将打开浏览器 帐户并授权访问 我
  • 鉴于我的代码是开源的并且我在服务器上运行,并且我接受近乎原始的代码,那么对我来说最糟糕的情况是什么?

    我正在研究几个案例 在这些案例中 接受近乎原始的代码会容易得多 所以 如果你不能使用 lambda 你能对表达式做的最糟糕的事情是什么 以及如何做 如果不能使用 import 那么对执行的代码最糟糕的情况是什么 如何使用 不能使用 X 扫描
  • Python:多重分配与单独分配速度

    我一直在寻求从我的代码中挤出更多的性能 最近 在浏览时这个 Python 维基页面 https wiki python org moin PythonSpeed 我发现了这个说法 多重分配比单独分配慢 例如 x y a b 比 x a y
  • 将 numpy 记录数组转换为字典列表的有效方法

    如何转换下面的 numpy 记录数组 recs Bill 31 260 0 Fred 15 145 0 r rec fromrecords recs names name age weight formats S30 i2 f4 到字典列表
  • 为什么 Python 布尔值占用超过一个字节?

    显然 Python 中整数占用 24 个字节 我可以理解 它这样做是因为代表无限数字的额外花哨 然而 布尔数据类型看起来也花费了高达 24 个字节 尽管它只能表示两个可能值之一 为什么 除了 1 位表示之外 还可能需要存储哪些额外数据Tru
  • 如何使用 Misc.imread 将图像分割为红色、绿色和蓝色通道

    我正在尝试将图像切片为 RGB 但在绘制这些图像时遇到问题 我使用此函数从某个文件夹获取所有图像 def get images path image type image list for filename in glob glob pat
  • 使用 PyQt5 拖放 QLabels

    我正在尝试使用 PyQt5 将 Qlabel 拖放到另一个 Qlabel 上 from PyQt5 QtWidgets import QApplication QWidget QToolTip QPushButton QMessageBox
  • Twitter 不再使用请求库 python

    我有一个 python 函数 它使用 requests 库和 BeautifulSoup 来抓取特定用户的推文 import requests from bs4 import BeautifulSoup contents requests
  • 多行 x 刻度标签

    我正在尝试制作类似于此 Excel 示例的图 我想知道 x 刻度标签上是否有第二层 例如 5 年统计摘要 我知道我可以使用制作多行刻度标签 n但我希望能够独立地转换这两个级别 这很接近 fig plt figure figsize 8 4

随机推荐