我是selenium的新手,我在chrome和firefox中通过身份验证弹出窗口时遇到问题。任何人都可以建议我处理它的最好方法。
我尝试在网址中提供凭据,但是当每个要加载的网页都要求凭据时,它会变得越来越难,有时它也不起作用。
答案 0 :(得分:0)
以下Robot实现在身份验证对话框打开时执行以下操作:
JAVA中的解决方案:
//Call this function and provide UserName and password
public void authenticate(String password,String Uname) throws AWTException, InterruptedException
{
Thread.sleep(5000);
Robot rb=new Robot();
StringSelection stringSelection = new StringSelection(Uname);
//Copy Path on Clipboard
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(stringSelection, null);
//Paste Clipboard Data
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
stringSelection = new StringSelection(password);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
}