如何使用selenium webdriver java切换到这个弹出屏幕?

问题描述 投票:1回答:3

方案是,当我点击它时有一个按钮,这个弹出窗口作为附加图像打开。我应该填写其中的数据,然后单击“提交”按钮。我无法使用以下代码切换到此弹出窗口:

    Set<String> handles = driver.getWindowHandles(); // get all window handles
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
    }
    driver.switchTo().window(subWindowHandler); // switch to popup window

任何帮助将不胜感激。

enter image description here

maven selenium selenium-webdriver
3个回答
0
投票

启动浏览器后,您可以尝试使用此代码:

由于没有窗口/警报,因此无需切换。

打开的弹出窗口只是一个Div。

        driver.manage().window().maximize();
        driver.get("http://41.33.122.55:3333/users/login");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("username"))).sendKeys("alaatest");
        wait.until(ExpectedConditions.elementToBeClickable(By.id("password"))).sendKeys("QWERTY123");  
        WebElement loginButton =   wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-white-rounded.login-btn")));  

        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].click();", loginButton);

        WebElement creaetAccount = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Add new Account']")));
        executor.executeScript("arguments[0].click();", creaetAccount);

        wait.until(ExpectedConditions.elementToBeClickable(By.name("firstName"))).sendKeys("Ali");

0
投票

根据我自己的经验,到达“新”窗口句柄需要几毫秒。在Firefox中,这对我有用:

public void goto2 () throws InterruptedException {
    Thread.sleep(100);
    ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
    Thread.sleep(100);
    driver.switchTo().window(winHandles.get(1));
}

在Chrome中,您甚至不需要切换,驱动程序自己完成工作。至少在新的标签和窗口,不确定是否在框架上。

如果你想尝试Chrome,有我的设置:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);
driver.manage().window().maximize();

它包含现有的Chrome个人资料,因此您可以使用扩展,缓存等等。只需编辑exe和profile文件夹的路径。


0
投票

完成登录和在模态窗口中到达firstName字段的代码。你相信自己管理的其余部分。

package navi; // rename this

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Egypt {

    public static WebDriver driver;

    public WebDriverWait wait1s = new WebDriverWait(driver,1);
    public WebDriverWait wait5s = new WebDriverWait(driver,5);
    public WebDriverWait wait10s = new WebDriverWait(driver,10);
    public WebDriverWait wait30s = new WebDriverWait(driver,30);
    public WebDriverWait wait1m = new WebDriverWait(driver,60);
    public WebDriverWait wait2m = new WebDriverWait(driver,120);

    @BeforeClass
    public static void setUpClass() {
        FirefoxOptions options = new FirefoxOptions();
        ProfilesIni allProfiles = new ProfilesIni();         
        FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
        options.setProfile(selenium_profile);
        options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.gecko.driver", Secrets_and_variables.get_driver_path());
        driver = new FirefoxDriver(options);
        driver.manage().window().maximize();
        }

    @Before
    public void setUp() {}

    @After
    public void tearDown() {}

    @AfterClass
    public static void tearDownClass() {driver.quit();}

    @Test
    public void egypt() throws InterruptedException {

        driver.get("http://41.33.122.55:3333/users/login");
        WebElement fld_username = wait30s.until(ExpectedConditions.elementToBeClickable(By.id("username")));
        fld_username.sendKeys("alaatest");
        WebElement fld_pwd = driver.findElement(By.id("password"));
        fld_pwd.sendKeys("QWERTY123");
        WebElement btn_login = driver.findElement(By.xpath("/html/body/app-root/div[3]/account-management/login/div[1]/div[2]/div[2]/form/div[3]/button"));
        btn_login.click();
        wait30s.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-root/div[3]/request-management/div[2]/accounts-list/div/table/tbody")));
        WebElement btn_add_acc = driver.findElement(By.xpath("/html/body/app-root/div[3]/request-management/div[1]/div/div[3]/a"));
        btn_add_acc.click();
        WebElement fld_firstName = wait5s.until(ExpectedConditions.elementToBeClickable(By.name("firstName")));
        fld_firstName.sendKeys("John");
        Thread.sleep(10000);

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.