在java selenium中处理窗口模式弹出

Handling window modal pop up in java selenium

提问人:Bug Hunter 提问时间:11/2/2023 最后编辑:YaroslavmBug Hunter 更新时间:11/4/2023 访问量:35

问:

我正在尝试关注 lambdatest 网站的 twitter 句柄,https://www.lambdatest.com/selenium-playground/window-popup-modal-demo Modal pop 成功启动,但此后我无法继续,这是我的代码,'我尝试过使用 driver.switch.to.iframe 仍然有同样的问题,切换到警报在这里不起作用,这是一个模态弹出窗口,但在 dic 中,模态弹出元素没有出现。

package lambdatest;
import java.time.Duration;
import java.util.Set;
import java.util.Iterator;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;`

public class Lambdatestplayground {

    public static void main(String[] args) {
        // to navigate to the website
        System.setProperty("webdriver.chrome.driver", "Path to chromedriver");
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://www.lambdatest.com/selenium-playground/window-popup-modal-demo");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(7));
        driver.findElement(By.xpath("//*[@id=\"__next\"]/section[3]/div/div/div[1]/div/div[1]/a")).click();
    }
}
Java Selenium-Web 自动化 Web驱动程序

评论


答:

0赞 Bhairu 11/2/2023 #1
> 1- To dismiss the Popup use the below code:

Webdriver wait;
     wait.until(ExpectedConditions.alertIsPresent());

        // Storing the alert in variable
        Alert alert = driver.switchTo().alert();

        alert.dismiss();

> 2- To Accept the popup use the below code:
Webdriver wait;
Alert alrt = wait.until(ExpectedConditions.alertIsPresent());

        // Sending the data into the alert popup
        alrt.sendKeys(input);

        // press the OK button
        alrt.accept();

评论

0赞 Bug Hunter 11/3/2023
我已经知道如何关闭它,但我想通过单击Twitter弹出窗口中的“关注”按钮来处理这个问题,感谢您的回答