任何人都可以告诉我如何处理使用java在selenium中弹出的窗口(保存用户名和密码)

时间:2017-04-14 09:48:49

标签: selenium-webdriver

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");
}

3 个答案:

答案 0 :(得分:1)

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

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 :(得分:1)

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

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");
}

答案 2 :(得分:1)

以下是您问题的解决方案:

  1. 根据标准做法,建议不要使用Thread.sleep(1000)而是使用Implicitwait,如下所示:
  2. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

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

      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);
      
    2. 将以下代码与Chrome驱动程序选项配合使用可帮助您摆脱所有问题。

      如果这有助于您,请告诉我。