【问题标题】:Need to select a checkbox in WebDriver需要在 WebDriver 中选中一个复选框
【发布时间】:2020-03-11 13:39:55
【问题描述】:

我需要勾选添加 GPU 复选框。网站https://cloud.google.com/products/calculator 有代码

WebElement checkAddGRU = new WebDriverWait(driver, 20)
            .until(ExpectedConditions.
                    presenceOfElementLocated(By.xpath("//md-input-container/md-checkbox/div[contains(text(),'Add GPUs')]")));
    WebElement numberOfGPU = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.elementToBeClickable(By.xpath("//md-select[@placeholder='Number of GPUs']")));
    while (!numberOfGPU.isDisplayed()) {
        checkAddGRU.click();
    }

但它不起作用。复选框最少为空。

【问题讨论】:

    标签: java selenium testing selenium-webdriver automated-tests


    【解决方案1】:

    您无法选择复选框的原因是 iframe 中存在该复选框。尝试先切换到 iframe,然后选中复选框。

    WebElement iframe = driver.findElement(By.cssSelector("#myFrame"));
    driver.switchTo().frame(iframe); 
    driver.findElement(By.xpath("//md-input-container/md-checkbox/div[contains(text(),'Add GPUs')]").click()
    

    截图:

    【讨论】:

    • 谢谢。对不起,没有说明。我以前就是这样切换到框架的。 driver.switchTo().frame(0); driver.switchTo().frame("myFrame");
    猜你喜欢
    • 2016-05-29
    • 2012-05-31
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多