如何使用python(加上java)处理selenium中的windows身份验证弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40671662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:51:37  来源:igfitidea点击:

How to handle windows authentication popup in selenium using python(plus java)

javapythonselenium

提问by Shoaib Akhtar

enter image description here

在此处输入图片说明

I tried the below code, but it didn't work for me

我尝试了以下代码,但对我不起作用

from selenium import webdriver
driver=webdriver.Chrome('D:/BrowsersDriver/chromedriver.exe')
driver.get('https://username:[email protected]/')

Later on I tried to use the same approach in Java

后来我尝试在 Java 中使用相同的方法

driver.get('https://username:[email protected]/')

driver.get(' https://username:[email protected]/')

But unfortunately it didn't work for me in any browser. Am I missing something here?

但不幸的是,它在任何浏览器中都不适用于我。我在这里错过了什么吗?

Then I tried with AutoIT in Java

然后我尝试在 Java 中使用 AutoIT

Runtime.getRuntime().exec("D:\FirefoxWindowAuthentication.exe");
driver.get("https://www.engprod-charter.net/")

It works well in Firefox & IE, but didn't work for Chrome. Is there any way that at-least I can achieve this in selenium using python & what I am missing in case of Java. Please suggest me any solution, tried a lot

它在 Firefox 和 IE 中运行良好,但不适用于 Chrome。有什么方法至少我可以使用 python 在 selenium 中实现这一点,以及在 Java 的情况下我缺少什么。请建议我任何解决方案,尝试了很多

采纳答案by Shoaib Akhtar

I got this solution which is working well for all three browser(Firefox, Chrome and IE).

我得到了这个解决方案,它适用于所有三种浏览器(Firefox、Chrome 和 IE)。

from selenium import webdriver
import time
import win32com.client

driver=webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.engprod-charter.net/')
shell = win32com.client.Dispatch("WScript.Shell")   
shell.Sendkeys("username")  
time.sleep(2)
shell.Sendkeys("{TAB}")
time.sleep(2)
shell.Sendkeys("password") 
time.sleep(2)
shell.Sendkeys("{ENTER}")
time.sleep(2)
driver.quit()

Note : Install win32com.client if you have not downloaded. To install win32com.client use below command

注意:如果您还没有下载,请安装 win32com.client。要安装 win32com.client 使用以下命令

pip install pypiwin32

回答by Andersson

Try following solution and let me know in case of any issues:

尝试以下解决方案,如有任何问题,请告诉我:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
import time

driver=webdriver.Firefox()
driver.get('https://www.engprod-charter.net/')
time.sleep(3)
driver.switch_to.alert.send_keys(username + Keys.TAB + password)
time.sleep(3)
driver.switch_to.alert.accept()

Hereis the solution for IE on Windows using third-party autoHK lib

是使用第三方 autoHK lib 在 Windows 上的 IE 解决方案