C# 如何单击复选框

C# 如何单击复选框,c#,selenium,checkbox,C#,Selenium,Checkbox,我的目标是选中网站上的复选框。html: <label class="checkbox" for="are_terms_agreed"> <input name="terms" id="are_terms_agreed" class="checkbox__input required-entry validate-checkbox" value="1" type="checkbox"> <span class="checkbox__span"> <i c

我的目标是选中网站上的复选框。html:

<label class="checkbox" for="are_terms_agreed">
<input name="terms" id="are_terms_agreed" class="checkbox__input required-entry validate-checkbox" value="1" type="checkbox">
<span class="checkbox__span">
<i class="checkbox__icon"></i>
</span>
<span class="checkbox__title">
I have read and agree to the 
<span class="signup__label-terms">
<a onclick="return false;" href="https://shop.adidas.ae/en/help/terms-conditions.html" class="js-dialog-btn" data-target="js-dialog-terms">
Terms &amp; Conditions
</a>
for website use
</span>
</span>
</label>

我已阅读并同意
供网站使用
原始链接:(您可能需要将鞋添加到购物车,然后继续结账过程以查看我正在查看的页面)

我尝试了几种不同的方法来实现这一点。 首先,我尝试访问

IWebElement termsandconditions2=driver.FindElement(By.XPath(“//label[@class='checkbox']//label[@for='are\u terms\u agreed']”)

我得到一个错误:

WebDriver.dll中发生类型为“OpenQA.Selenium.NoSuchElementException”的未处理异常 其他信息:找不到元素:{“方法”:“xpath”,“选择器”:“//label[@class='checkbox']//label[@for='are_terms.\u agreed']”}

我也尝试过:

IWebElement termsandconditions=driver.FindElement(由.CssSelector(“span[class*='checkbox\uu span'])

这基本上是点击实际的“检查”图标,而不是像我以前尝试的那样检查整个框。当我运行此操作时,我会得到以下错误:

WebDriver.dll中发生类型为“OpenQA.Selenium.ElementNotVisibleException”的未处理异常。其他信息:元素当前不可见,因此可能无法与之交互

我尝试过使用向下滚动代码,但什么也没发生


有什么帮助吗?谢谢

根据坐标单击事件。试试看

Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.Xpath("//*[@id='co-terms-form']/label[1]/span[1]")), 2, 2).Click().Perform();
或:


根据坐标单击事件。试试看

Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.Xpath("//*[@id='co-terms-form']/label[1]/span[1]")), 2, 2).Click().Perform();
或:


它有“ID”,使用
driver.FindElement(By.ID(“是否同意条款”))。单击()错误:其他信息:元素当前不可见,因此可能无法与try driver.FindElement交互(通过.CssSelector(“#共同术语表单>标签:第n个子项(2)>span.checkbox_uspan>i”);它有“ID”,使用
driver.FindElement(By.ID(“是否同意条款”))。单击()错误:其他信息:元素当前不可见,因此可能无法与try driver.FindElement交互(通过.CssSelector(“#共同术语表单>标签:第n个子项(2)>span.checkbox_uspan>i”);嗨,特古特,我在[1]里面放了什么?还是我就这样离开?就这样离开:)。“[1]”索引计数。但是XPath的所有web元素都是不同的。不管怎样,完全解决了这个问题。非常感谢你,伙计!你好,特古特,我在[1]里面放了什么?还是我就这样离开?就这样离开:)。“[1]”索引计数。但是XPath的所有web元素都是不同的。不管怎样,完全解决了这个问题。非常感谢你,伙计!你很好:)