我正在尝试测试此网站上的隐私声明复选框
复选框的HTML代码是
<input type="checkbox" name="registryTandCs" id="registryTandCs" value="" class="form-check-input form-check-input--custom" required="required" data-parsley-multiple="registryTandCs">
我用来检查的代码是
driver.findElement(By.id("registryTandCs")).click();
但是,它似乎不起作用。 我也试过
WebElement checkBox = driver.findElement(By.id("registryTandCs"));
checkBox.click();
如果有任何帮助,有问题的网站是 here
答案 0 :(得分:1)
主要问题是当您尝试单击复选框时,其他元素被单击。您可以看到输入和标签标签重叠(请检查附加的图片)。
截图:
始终检查错误堆栈跟踪。
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element
<input type="checkbox" name="registryTandCs" id="registryTandCs" value="" class="form-check-input form-check-input--custom" required="required" data-parsley-multiple="registryTandCs">
is not clickable at point (655, 517).
Other element would receive the click:
<label for="registryTandCs" class="form-check-label form-check-label--grey">...</label>
可行的解决方案:有两种方法可以解决这个问题。
使用操作类:
WebElement checkBox = driver.findElement(By.id("registryTandCs"));
Actions action = new Actions(driver);
action.moveToElement(checkBox).click().build().perform();
使用 JavascriptExecutor:
WebElement checkBox = driver.findElement(By.id("registryTandCs"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", checkBox);
答案 1 :(得分:0)
对于元素上的 click()
,您可以使用以下任一 Locator Strategies:
id:
driver.findElement(By.id("registryTandCs")).click();
姓名:
driver.findElement(By.name("registryTandCs")).click();
cssSelector:
driver.findElement(By.cssSelector("input#registryTandCs[name='registryTandCs']")).click();
xpath:
driver.findElement(By.xpath("//input[@id='registryTandCs' and @name='registryTandCs']")).click();
最好在需要为 click()
引入 WebDriverWait 的元素上设置 elementToBeClickable()
,您可以使用以下任一 Locator Strategies:
id:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("registryTandCs"))).click();
姓名:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("registryTandCs"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#registryTandCs[name='registryTandCs']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='registryTandCs' and @name='registryTandCs']"))).click();