无法在python中使用selenium打开IE

时间:2017-08-22 21:11:40

标签: python internet-explorer selenium-webdriver

我在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"])

输出:

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

更新: 我也尝试过设置功能但仍然收到同样的错误:&#34;未知功能名为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/")

1 个答案:

答案 0 :(得分:3)

我有几天同样的问题。 我的解决方法是从platform词典中删除versioncapabilities个键

示例:

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是唯一正确的功能。您可以在Traceback中看到:

  

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

当您点击它时,您将看到:

w3c_caps["alwaysMatch"].update(capabilities) 

正如您所看到的,here _W3C_CAPABILITY_NAMES拥有的值与我们传递的值不同。 我们将“WINDOWS”作为“平台”传递,而_W3C_CAPABILITY_NAMES具有“platformName”并且仅接受小型上限。 “版本”键也是如此。

因此我们添加了无法识别的功能。

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

编辑:Grimlek评论中可以找到另一个解决方案here,其实质上是说您应该从"capabilities": w3c_caps(来自remote \ webdriver)删除start_session(self, capabilities, browser_profile=None)。 PY)。代码如下所示:

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

然后您不需要从功能中删除密钥。

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