输入用户名和密码时出现问题

时间:2019-04-12 16:18:39

标签: java selenium-webdriver

我正在尝试访问要求输入用户名和密码的网站。我仅在Chrome中遇到此问题。对于Firefox开关正常工作。 enter image description here

 driver.navigate().to("https://t...............");
    driver.switchTo().alert().sendKeys("@@@@@" + Keys.TAB + "******");
    driver.switchTo().alert().accept();
     OR
       Robot robot = new Robot(); 
       robot.delay(1500);
       robot.keyPress(KeyEvent.VK_Q); 

2 个答案:

答案 0 :(得分:0)

var alert = driver.SwitchTo().Alert();

alert.SetAuthenticationCredentials("username", "password");

alert.Accept();

答案 1 :(得分:0)

尝试:-

 WebDriver driver =new FirefoxDriver();
 driver.get("URL");
 Robot rb = new Robot();

 //Enter user name by ctrl-v
 StringSelection username = new StringSelection("username");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);            
 rb.keyPress(KeyEvent.VK_CONTROL);
 rb.keyPress(KeyEvent.VK_V);
 rb.keyRelease(KeyEvent.VK_V);
 rb.keyRelease(KeyEvent.VK_CONTROL);

 //tab to password entry field
 rb.keyPress(KeyEvent.VK_TAB);
 rb.keyRelease(KeyEvent.VK_TAB);
 Thread.sleep(2000);

 //Enter password by ctrl-v
 StringSelection pwd = new StringSelection("password");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
 rb.keyPress(KeyEvent.VK_CONTROL);
 rb.keyPress(KeyEvent.VK_V);
 rb.keyRelease(KeyEvent.VK_V);
 rb.keyRelease(KeyEvent.VK_CONTROL);

 //press enter
 rb.keyPress(KeyEvent.VK_ENTER);
 rb.keyRelease(KeyEvent.VK_ENTER); 

使用机器人类可以处理弹出窗口。

希望这对您有帮助!

尝试一下:-

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver =  new ChromeDriver(options);
driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");