谁能告诉我如何使用Java处理Selenium中弹出的Windows(保存用户名和PWD)


public static void main(String[] args) throws InterruptedException 
{
    System.setProperty("webdriver.chrome.driver","D:/chrome driver/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.get("http://app.mycable.in/#/login");
    driver.findElement(By.xpath("//input[@name='username']")).sendKeys("mycable");
    driver.findElement(By.xpath("//input[@name='password']")).sendKeys("mycable1");
    driver.findElement(By.xpath("//input[@name='submit']")).click();
    Thread.sleep(1000);
    Alert alert =driver.switchTo().alert();
    alert.accept();
    driver.findElement(By.xpath(".//a[contains(href='#/customer/')][text='restricted.customer']")).click();
    driver.findElement(By.xpath("//input[@value='input']")).sendKeys("abcd");
}

只需在Chrome中使用隐身模式即可避免任何警报/通知,隐身在某些方面也是安全的。只需运行以下代码。

public static void main(String[] args) throws InterruptedException          
{
 ChromeOptions options = new ChromeOptions();
 options.addArguments("--incognito");
 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
 capabilities.setCapability(ChromeOptions.CAPABILITY, options);
 WebDriver driver=new ChromeDriver(capabilities);
 driver.get("http://app.mycable.in/#/login");
 driver.get("http://app.mycable.in/#/login");
 driver.findElement(By.xpath("//input[@name='username']")).sendKeys("mycable");
 driver.findElement(By.xpath("//input[@name='password']")).sendKeys("mycable1");
 driver.findElement(By.xpath("//input[@name='submit']")).click();
 Thread.sleep(1000);
 driver.findElement(By.xpath(".//a[contains(href='#/customer/')][text='restricted.customer']")).click();
 driver.findElement(By.xpath("//input[@value='input']")).sendKeys("abcd");
}

为了避免记住用户名和密码弹出,这些代码段可能会对您有所帮助。

DesiredCapabilities capabilities = new DesiredCapabilities();    
capabilities = DesiredCapabilities.chrome();
chromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
每次都

不使用保存密码,因为在每次执行时都会调用驱动程序的新实例,其中没有 cookie 或缓存。如果有任何问题,请告诉我

这是您问题的解决方案:

  1. 根据标准做法,建议不要使用 Thread.sleep(1000)而是使用 Implicitwait,如下所示:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

  1. 保存用户名/密码不是警报。我们将使用Chrome的选项类禁用它,如下所示:

    System.setProperty("webdriver.chrome.driver", "C:\SeleniumUtilities\BrowserDrivers\chromedriver.exe");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values.notifications", 2);
    prefs.put("credentials_enable_service", false);
    prefs.put("profile.password_manager_enabled", false);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    options.addArguments("--disable-extensions");
    options.addArguments("--disable-notifications");
    options.addArguments("--enable-automation");
    options.addArguments("--disable-save-password-bubble");
    options.addArguments("test-type");
    options.addArguments("start-maximized");
    options.addArguments("test-type=browser");
    options.addArguments("disable-infobars");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(capabilities);
    driver.get("http://app.mycable.in/#/login");
    driver.findElement(By.xpath("//input[@name='username']")).sendKeys("mycable");
    driver.findElement(By.xpath("//input[@name='password']")).sendKeys("mycable1");
    driver.findElement(By.xpath("//input[@name='submit']")).click();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    

将以下代码与Chrome驱动程序选项一起使用将帮助您摆脱所有问题。

让我知道这是否对您有帮助。

最新更新