Selenium webdriver 谁能告诉我如何使用java处理selenium中的windows弹出窗口(保存用户名和pwd)

Selenium webdriver 谁能告诉我如何使用java处理selenium中的windows弹出窗口(保存用户名和pwd),selenium-webdriver,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("htt

为了避免出现“记住用户名和密码”弹出窗口,这些代码可能对您有帮助

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");
}
DesiredCapabilities=新的DesiredCapabilities();
能力=DesiredCapabilities.chrome();
chromeOptions选项=新的chromeOptions();
Map prefs=新的HashMap();
prefs.put(“凭证启用服务”,false);
prefs.put(“profile.password\u manager\u enabled”,false);
选项。设置实验选项(“prefs”,prefs);
能力。设置能力(ChromeOptions.CAPABILITY,选项);
WebDriver=新的ChromeDriver(功能);

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

,以避免出现“记住用户名和密码”弹出窗口。这些代码可能对您完全有帮助

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");
}
DesiredCapabilities=新的DesiredCapabilities();
能力=DesiredCapabilities.chrome();
chromeOptions选项=新的chromeOptions();
Map prefs=新的HashMap();
prefs.put(“凭证启用服务”,false);
prefs.put(“profile.password\u manager\u enabled”,false);
选项。设置实验选项(“prefs”,prefs);
能力。设置能力(ChromeOptions.CAPABILITY,选项);
WebDriver=新的ChromeDriver(功能);

每次保存密码是没有用的,因为每次执行时都会调用驱动程序的新实例,其中没有cookie或缓存。如果有任何问题,请告诉我,只需在Chrome中使用Incognito模式,以避免任何警报/通知,Incognito在某些方面也是安全的。 只需运行下面的代码

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

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

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

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

  • 根据标准实践,建议不要使用
    Thread.sleep(1000)
    而使用Implicitwait,如下所示:
  • driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS)

  • 保存用户名/密码不是警报。我们将使用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");
    }
    
    System.setProperty(“webdriver.chrome.driver”,“C:\\seleniumartilities\\BrowserDrivers\\chromedriver.exe”);
    Map prefs=新的HashMap();
    prefs.put(“profile.default\u content\u setting\u values.notifications”,2);
    prefs.put(“凭证启用服务”,false);
    prefs.put(“profile.password\u manager\u enabled”,false);
    ChromeOptions选项=新的ChromeOptions();
    选项。设置实验选项(“prefs”,prefs);
    options.addArguments(“--disable extensions”);
    options.addArguments(“--disable notification”);
    options.addArguments(“--enable automation”);
    options.addArguments(“--disable save password bubble”);
    options.addArguments(“测试类型”);
    选项。添加参数(“开始最大化”);
    options.addArguments(“测试类型=浏览器”);
    options.addArguments(“禁用信息栏”);
    DesiredCapabilities=DesiredCapabilities.chrome();
    能力。设置能力(ChromeOptions.CAPABILITY,选项);
    WebDriver=新的ChromeDriver(功能);
    驱动程序。获取(“http://app.mycable.in/#/login");
    findElement(By.xpath(//input[@name='username'])).sendKeys(“mycable”);
    findElement(By.xpath(//input[@name='password'])).sendKeys(“mycable1”);
    findElement(By.xpath(//input[@name='submit'])。单击();
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    
  • 在Chrome驱动程序选项中使用以下代码将帮助您解决所有问题


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

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

  • 根据标准实践,建议不要使用
    Thread.sleep(1000)
    而使用Implicitwait,如下所示:
  • driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS)

  • 保存用户名/密码不是警报。我们将使用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");
    }
    
    System.setProperty(“webdriver.chrome.driver”,“C:\\seleniumartilities\\BrowserDrivers\\chromedriver.exe”);
    Map prefs=新的HashMap();
    prefs.put(“profile.default\u content\u setting\u values.notifications”,2);
    prefs.put(“凭证启用服务”,false);
    prefs.put(“profile.password\u manager\u enabled”,false);
    ChromeOptions选项=新的ChromeOptions();
    选项。设置实验选项(“prefs”,prefs);
    options.addArguments(“--disable extensions”);
    options.addArguments(“--disable notification”);
    options.addArguments(“--enable automation”);
    options.addArguments(“--disable save password bubble”);
    options.addArguments(“测试类型”);
    选项。添加参数(“开始最大化”);
    options.addArguments(“测试类型=浏览器”);
    options.addArguments(“禁用信息栏”);
    DesiredCapabilities=DesiredCapabilities.chrome();
    能力。设置能力(ChromeOptions.CAPABILITY,选项);
    WebDriver=新的ChromeDriver(功能);
    驱动程序。获取(“http://app.mycable.in/#/login");
    findElement(By.xpath(//input[@name='username'])).sendKeys(“mycable”);
    findElement(By.xpath(//input[@name='password'])).sendKeys(“mycable1”);
    findElement(By.xpath(//input[@name='submit'])。单击();
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    
  • 在Chrome驱动程序选项中使用以下代码将帮助您解决所有问题

    如果这对你有帮助,请告诉我