【发布时间】:2016-10-14 22:21:45
【问题描述】:
我的目标是选中网站上的复选框。 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 & Conditions
</a>
for website use
</span>
</span>
</label>
原始链接:https://shop.adidas.ae/en/checkout/onepage/(您可能需要将鞋子添加到购物车并进行结帐流程才能查看我正在查看的页面)
我尝试了几种不同的方法来实现这一点。 首先,我尝试访问
IWebElement termsandconditions2 = driver.FindElement(By.XPath("//label[@class = 'checkbox']//label[@for = 'are_terms_agreed']"));
我得到错误:
WebDriver.dll 中出现“OpenQA.Selenium.NoSuchElementException”类型的未处理异常 附加信息:无法定位元素:{"method":"xpath","selector":"//label[@class= 'checkbox']//label[@for = 'are_terms_agreed']"}
我也试过了:
IWebElement termsandconditions = driver.FindElement(By.CssSelector("span[class*='checkbox__span']"));
这基本上是单击实际的“检查”图标,而不是像我之前尝试的那样检查整个框。当我运行它时,我得到了错误:
WebDriver.dll 中出现“OpenQA.Selenium.ElementNotVisibleException”类型的未处理异常附加信息:元素当前不可见,因此可能无法与之交互
我尝试使用向下滚动代码,没有任何反应。
有什么帮助吗?谢谢
【问题讨论】:
-
它有'ID',使用
driver.FindElement(By.Id("are_terms_agreed")).Click(); -
错误:附加信息:元素当前不可见,因此可能无法与之交互
-
试试 driver.FindElement(By.CssSelector("#co-terms-form > label:nth-child(2) > span.checkbox__span > i"));