繁体   English   中英

如何使用Java通过Selenium WebDriver处理fancybox弹出窗口

[英]How to handle fancybox popup through Selenium WebDriver using Java

我正在尝试使用以下代码处理身份验证弹出窗口:

driver.get("https://www.printvenue.com");
System.out.println("Successfully opened the Printvenue");
driver.manage().window().maximize();
driver.findElement(By.id("login_li")).click();
Thread.sleep(2000);
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().window(child);
driver.findElement(By.id("email")).sendKeys("abcd@gmail.com");

但是我无法在电子邮件测试框中输入电子邮件。 请帮忙。

您没有可切换到的窗口,因此无需使用该开关。

您的登录弹出窗口已附加到主DOM,因此您可以直接在其中编写。 因为您的元素根本不是唯一的,所以您将必须使用findElements方法。

我已经在Firefox中测试了以下代码,并且可以正常工作:

driver.get("https://www.printvenue.com");
System.out.println("Successfully opened the Printvenue");
driver.manage().window().maximize();
driver.findElement(By.id("login_li")).click();
Thread.sleep(2000);
List<WebElement> emailElement = driver.findElements(By.id("email"));
System.out.println(emailElement.size()); // this will tell you how many elements with this ID you have in your DOM
emailElement.get(3).sendKeys("abcd@gmail.com");

这不是弹出窗口,而是Lightbox

幸运的是,这使得处理起来非常容易,它只是标准DOM中的标准HTML。 您的问题的解决方案是:

    WebDriverWait wait = new WebDriverWait(driver, 15, 100);
    driver.get("https://www.printvenue.com");

    System.out.println("Successfully opened the Printvenue");

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login_li"))).click();
    WebElement emailElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
    emailElement.sendKeys("abcd@gmail.com");

好吧,这应该是解决您问题的任何方法。 您在这里看到的真正问题是页面上(在撰写本文时)有4个元素共享一个应该唯一的ID。 这是您的开发人员需要解决的问题,因为此HTML不符合W3C! 我会提出它作为一个错误,并让他们修复它。

您可以使用以下代码解决此问题:

    List<WebElement> emailElements = driver.findElements(By.id("email"));
    System.out.println(String.format("Oh dear, there are %s instances of the id email when there should only be 1...", emailElements.size()));
    emailElements.get(3).sendKeys("abcd@gmail.com");

但是,我鼓励您不要这样做,这确实需要修复!

您所指的身份验证弹出窗口在技​​术上称为fancybox 要在“ 电子邮件”字段中发送字符序列 ,您需要为elementToBeClickable()引入WebDriverWait ,并且可以使用以下解决方案:

  • 代码块:

     System.setProperty("webdriver.chrome.driver", "C:\\\\Utility\\\\BrowserDrivers\\\\chromedriver.exe"); ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("start-maximized"); //chromeOptions.addArguments("disable-infobars"); chromeOptions.addArguments("--disable-extensions"); WebDriver driver = new ChromeDriver(chromeOptions); driver.get("https://www.printvenue.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Login"))).click(); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.fancybox-outer input[id='email']"))).sendKeys("abcd@gmail.com"); 
  • 浏览器快照:

打印场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM