【问题标题】:Second checkbox click is ignored第二个复选框单击被忽略
【发布时间】:2018-05-27 23:53:21
【问题描述】:

以下代码忽略了第二次复选框单击。有谁知道为什么以及如何解决?在最后一行设置断点以查看复选框是否仍处于选中状态...

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)...
}

【问题讨论】:

  • 点击不会在您的代码中被忽略。我在“driver.findElement(CHECKBOX).click();//此点击被忽略”行之后放置了一个 Thread.sleep(10000),我可以看到该复选框第二次被点击并且它正在未选中。

标签: java selenium selenium-webdriver webdriver selenium-edgedriver


【解决方案1】:

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

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() 方法,以确定它是否真的被选中。如果选中,则该方法返回 true,否则返回 false。

【讨论】:

  • 我也注意到这种睡眠行为。我还实现了基于 isSelected 的 ExpectedCondition,但行为仍然相同。 (第二次点击被忽略)。似乎是 EdgeDriver 中的一个错误。
  • 好吧,当 selenium 命令无法在某些浏览器上执行所需的操作时,您可以使用 javascript(提供的浏览器支持它)自己处理。您可以在条件块中使用 .click 和 .checked 来执行所需的任务。 document.getElementById("target").checked;
【解决方案2】:

您需要注意以下几个事实:

  • FluentWait:使用 WebDriver 实例的 FluentWait 的定制专业化,而不是 FluentWait&lt;WebDriver&gt; 使用 WebDriverWait(),直到和除非绝对必要。

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

  • pageLoadTimeout():尽量避免配置pageLoadTimeout(),除非测试规范明确提及。

  • 一旦你switch() 到所需的帧而不是ExpectedConditions 作为elementToBeSelected() 等待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();
        }
    }
    
  • 浏览器快照:

【讨论】:

    【解决方案3】:

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

    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)...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2021-05-03
      相关资源
      最近更新 更多