簡體   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