无法在python中使用硒打开IE [英] Can't open IE using selenium in python

查看:109
本文介绍了无法在python中使用硒打开IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 10计算机,Internet Explorer 11,python 3.6,带有IEDriverServer 3.5的硒3.4.3上运行.我正在尝试使用以下代码打开IE.

I am running on a Windows 10 machine, Internet Explorer 11, python 3.6, selenium 3.4.3 with IEDriverServer 3.5. I am trying to open up IE using the following code.

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

任何帮助将不胜感激.

更新: 我将此添加到了以前的代码中,

UPDATE: I added this to my previous code,

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

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

UPDATE: I have also tried setting the capabilities but still recieve the same error: "unknown capabilities 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/")

推荐答案

几天来我遇到了同样的问题. 我的解决方法是从capabilities字典中删除platformversion

I had the same problem for couple of days. My workaround for this was to delete platform and version keys from capabilities dictionary

示例:

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作为唯一正确的功能进行了传递.您可以在回溯"中看到它:

My guess, so far, is that this error happens because w3c_caps are passed as the only right capabilities. You can see that in the Traceback:

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

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

when you click on it you will see that:

w3c_caps["alwaysMatch"].update(capabilities) 

您可以在此处查看 _W3C_CAPABILITY_NAMES具有与我们传递的值不同的值. 我们将"WINDOWS"作为平台"传递,而_W3C_CAPABILITY_NAMES具有"platformName",并且仅接受小写字母. 版本"键也是如此.

As you can see here _W3C_CAPABILITY_NAMES hold different values than the ones we are passing. We are passing "WINDOWS" as "platform", while _W3C_CAPABILITY_NAMES has "platformName" and accepts only small caps. Same goes for "version" key.

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

So we were adding capabilities that are not recognized.

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

This workaround is by no means perfect, and I was able to start IE in selenium java without deleting some of the capabilities.

此处在Grimlek注释中,该注释实际上表示您应该从start_session(self, capabilities, browser_profile=None)(从remote \ webdriver.py)中删除"capabilities": w3c_caps.代码如下:

Another solution can be found here in Grimlek comment, which essentially says that you should delete "capabilities": w3c_caps from start_session(self, capabilities, browser_profile=None) (from remote\webdriver.py). Code looks like this:

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

然后,您无需从功能中删除键.

Then you would not need to delete keys from capabilities.

另一项我刚刚将Selenium-python从3.4.3更新到了3.5.0,并且不再需要搞乱功能.

ANOTHER I have just updated my selenium-python from 3.4.3 to 3.5.0 and there's no longer need for messing with capabilities.

这篇关于无法在python中使用硒打开IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆