我目前无法使用Java中的Selenium WebDriver选中复选框(选中它)。我尝试了一切。通过Xpath,CssSelector,ID,Linktext,Partial LinkText定位... WebDriver会触发单击...但复选框仍未选中。
现在奇怪的是,当我用Selenium IDE录制它并播放时,它会检查复选框而没有任何问题。但是当我在java中使用相同的代码和定位器时......复选框不会被检查。
我尝试将其作为复选框处理,我尝试点击它,我尝试点击标签。但我无法让它发挥作用,它开始让我感到沮丧。
以下是我要访问的页面上复选框的代码:
<div class="accept-terms">
<div class="row">
<input id="sso-bonus-card-terms" name="cgvAccepted" class="checkbox-custom" data-msg-ssoBonusCardTerms="Veuillez accepter les conditions générales de SSO et Carrefour Bonus Card" type="checkbox" value="true"/><label for="sso-bonus-card-terms">J'accepte les <a href='#'>conditions générales</a> de SSO et Carrefour Bonus Card<span class='obligated'>*</span></label><input type="hidden" name="_cgvAccepted" value="on"/>
这是我现在可以访问它的代码...(我的项目中已经有一个中央Webdriver实例,因此页面上的元素已经定义并定位)
@FindBy(css = "#registrationForm > div.registration-form-wrapper > div.registration-box.bonus-points-payment > div > div > div > div.col-xs-10 > div > div.card-bonus-content.card-no > div.accept-terms > div > label:nth-child(2)")
private WebElement ssoBonusCardTerms;
public SubscriptionWizardPage acceptGeneralConditions(){
logger.info("Scroll down and accept the general conditions");
townCityInput.sendKeys(Keys.PAGE_DOWN);
focus.moveToElement(webDriver.findElement(By.xpath("//*[@id=\"registrationForm\"]/div[4]/div[6]/div/div"))).perform();
focus.moveToElement(webDriver.findElement(By.id("sso-bonus-card-terms"))).perform();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
focus.moveToElement(ssoBonusCardTerms).click().perform();
return new SubscriptionWizardPage();
}
答案 0 :(得分:1)
如果没有修复,请尝试更新您的selenium版本。试试这个:
WebElement elementChkBox= driver.findelement(By.cssSelector("input[type='checkbox']")
Webdriverwait wait = new Webdriverwait(driver,10)
wait.until(ExpectedConditions.elementToBeClickable(locator);
if(!elementChkBox.isSelected()) {
checkbox.click();
OR
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementChkBox);
}
答案 1 :(得分:0)
Am guessing there maybe sync issue on load, object is not visible when action perform, lets put some explicit wait
public SubscriptionWizardPage acceptGeneralConditions()
{
WebElement checkbox= driver.findelement(by.xpath("//input[@id="sso-bonus-card-terms"][@name="cgvAccepted"]")
Webdriverwait wait = new Webdriverwait(driver,10)
wait.until(ExpectedConditions.elementToBeClickable(locator);
checkbox.click();
}
回复结果
答案 2 :(得分:0)
我找到了解决方案:
JavascriptExecutor js =(JavascriptExecutor)webDriver; js.executeScript( “document.querySelector(\”[用于= 'SSO-奖金卡-术语'] \ “)单击();”);