第二次复选框点击被忽略

Second checkbox click is ignored

以下代码忽略了第二次复选框单击。任何人都知道为什么以及如何解决?在最后一行设置断点可以看到复选框仍然被选中...

private final static String CHECKBOXBUTTON_URL = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_Checkbox";
private final static String RESULT_IFRAME = "iframeResult";
private final static By CHECKBOX = By.xpath("/html/body/form/input[2]");

@Test
public void checkbox()
{
    System.setProperty("webdriver.edge.driver", "MicrosoftWebDriver.exe");
    WebDriver driver = new EdgeDriver();
    FluentWait<WebDriver> wait = new WebDriverWait(driver, 0);
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    wait.withTimeout(Duration.ofSeconds(10)).pollingEvery(Duration.ofMillis(200));

    driver.get(CHECKBOXBUTTON_URL);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(RESULT_IFRAME));

    driver.findElement(CHECKBOX).click();
    wait.until(ExpectedConditions.elementToBeSelected(CHECKBOX));

    driver.findElement(CHECKBOX).click(); // this click is ignored

    driver.quit(); // break here; checkbox is still checked (but shouldn't)...
}

您需要注意以下几点:

  • FluentWait:使用 FluentWait 而不是 FluentWait<WebDriver> use WebDriverWait() 的定制专业化 WebDriver 个实例,除非绝对必要。

  • 您需要将 WebDriverWait 配置为 timeOutInSeconds 所需的值,例如5、10、15 但不是 0.

  • pageLoadTimeout():尽量避免配置 pageLoadTimeout(),除非测试规范明确提到了差不多。

  • 一旦您 switch() 到所需的帧而不是 ExpectedConditionselementToBeSelected() 等待 elementToBeClickable() 如下:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='vehicle' and @value='Car']"))).click();
    
  • 你优化后的代码块如下:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Checkbox_Click {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\path\to\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml_Checkbox");
            new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframeResult"));
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='vehicle' and @value='Car']"))).click();
        }
    }
    
  • 浏览器快照:

由于浏览器的动画时间间隔而被忽略以在框中显示复选标记。

driver.findElement(CHECKBOX).click();
wait.until(ExpectedConditions.elementToBeSelected(CHECKBOX));
//Something can be coded here    
driver.findElement(CHECKBOX).click(); // this click is ignored

您可以选择以下两种说法中的一种。

  1. 添加 1 或 2 秒的硬等待。使用 Thread.sleep(2000) 。这将允许传递动画稳定下来。

  2. 在目标复选框上使用isSelected()方法,判断它是否真的被选中。方法 returns 如果选择则为真,否则为假。

您可以使用索引切换帧并使用它移动到第二帧,然后在复选框上执行操作。

private final static String CHECKBOXBUTTON_URL = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_Checkbox";
private final static By CHECKBOX = By.xpath("//input[@name='vehicle' and @value='Car']");

@Test
public void checkbox()
{
    System.setProperty("webdriver.edge.driver", "MicrosoftWebDriver.exe");
    WebDriver driver = new EdgeDriver();
    FluentWait<WebDriver> wait = new WebDriverWait(driver, 0);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.switchTo().frame(1); 
    //   new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframeResult"));
     WebElement element=driver.findElement(By.xpath("//input[@name='vehicle' and @value='Car']"));
    element.click();
    wait.until(ExpectedConditions.elementToBeSelected(element));
    element.click();
}

    driver.quit(); // break here; checkbox is still checked (but shouldn't)...
}