0

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

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

3 回答 3

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。

于 2018-05-28T05:37:38.447 回答
0

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

  • FluentWait:除非绝对必要,否则使用WebDriver实例的FluentWait的量身定制的特化,而不是FluentWait<WebDriver>use 。WebDriverWait()

  • 您需要使用所需的timeOutInSeconds值配置WebDriverWait ,例如 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();
        }
    }
    
  • 浏览器快照:

Browser_Snap_Second_Select

于 2018-05-28T05:18:22.663 回答
0

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

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)...
}
于 2018-05-28T06:03:25.193 回答