2017-07-26 46 views
3

我想在提示中輸入數據(URL給出),下面的代碼給我一個錯誤。請幫我解決這些問題?Python的Windows身份驗證用戶名和密碼不起作用

from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.keys import Keys 
import time 
driver = webdriver.Firefox() 
url = "http://the-internet.herokuapp.com/basic_auth" 
driver.get(url) 
time.sleep(5) 
alert = driver.switch_to.alert 
alert.authenticate('admin','admin') 
time.sleep(4) 
alert.accept() 

我曾嘗試用:

ActionChains(driver).send_keys("admin").send_keys(Keys.TAB).send_keys("admin").perform() 

這一個也不能正常工作。

+0

不知道這是你的問題,但我看到的第一件事情是你忘了「()」在戒備= driver.switch_to.alert () – Chai

+0

嘗試:'警報= driver.switch_to.alert() TypeError:'警報'對象不可調用 – jaibalaji

回答

5

當你與Selenium 3.4.0工作,geckodriver v0.18.0Mozilla Firefox 53.0通過Python 3.6.1您可以通過自身嵌入usernamepasswordurl如下繞過Basic Authentication彈出。

該解決方案將打開URLhttp://the-internet.herokuapp.com/basic_auth並且以有效usernamepassword證書認證。

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe') 
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe") 
driver.get("http://admin:[email protected]/basic_auth") 
+0

謝謝你:) :)它工作:) – jaibalaji

+0

當然。做過的男人。 – jaibalaji

+0

FWIW:嵌入憑證只能部分使用chrome v59 +工作,因爲子資源請求將被阻止。 – orde

0
def test_1_authentication(self): 
     self.driver.get("https://the-internet.herokuapp.com/basic_auth") 
     shell = win32com.client.Dispatch("WScript.Shell") 
     shell.Sendkeys("admin") 
     time.sleep(3) 
     shell.Sendkeys("{TAB}") 
     time.sleep(3) 
     shell.Sendkeys("admin") 
     time.sleep(3) 
     shell.Sendkeys("{ENTER}") 
     time.sleep(3) 

上面的代碼也工作正常:)

相關問題