2017-08-22 124 views
3

我在Windows 10計算機,Internet Explorer 11,python 3.6,使用IEDriverServer 3.5的Selenium 3.4.3上運行。我試圖用下面的代碼打開IE。無法在python中使用硒打開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/AAG_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\\JDroetti\\PycharmProjects\\Lib\\IEDriverServer.exe") 
browser.get("https://www.facebook.com/") 

回答

2

我有幾天的同樣的問題。 我對這個解決辦法是從capabilities詞典中刪除platformversion

例子:

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 _W3C_CAPABILITY_NAMES持有超過我們傳遞的那些不同的值。 我們通過「WINDOWS」作爲「平臺」,而_W3C_CAPABILITY_NAMES具有「platformName」並且只接受小型大寫字母。 「版本」鍵也一樣。

因此,我們添加了無法識別的功能。

這種解決方法並不完美,我能夠在不刪除某些功能的情況下在selenium java中啓動IE。

編輯:另一種解決方案可以在Grimlek評論,基本上說,你應該從start_session(self, capabilities, browser_profile=None)(遠程\ webdriver.py)刪除"capabilities": w3c_caps找到here。代碼如下所示:

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

然後,您不需要從功能中刪除鍵。

其他編輯:我剛剛將我的硒-python從3.4.3更新到了3.5.0,並且不再需要搞亂功能。

相關問題