首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C# for Selenium选中多个框

如何使用C# for Selenium选中多个框
EN

Stack Overflow用户
提问于 2021-10-18 16:57:33
回答 1查看 70关注 0票数 0

在尝试使用C#运行Selenium测试时,我遇到了一个问题,无法同时选中这两个“复选框”。

这是我为第一个复选框准备的,它确实有效,并选中了该框。driver.FindElement(By.XPath("//label@for='LegitimateCompanyAgreementCheckBox'")).Click();

但是,如果我简单地使用另一个复选框的标签"TermsOfServiceCheckBox",它不会选中该复选框,但它实际上会打开一个链接,因为这个复选框的措辞中有两个超链接。

例如,如果我尝试使用: driver.FindElement(By.XPath("//label@for='TermsOfServiceCheckBox'")).Click();,这将不会选中该框,它实际上会打开/terms-of-service的链接。

下面是我需要检查的两个复选框的代码。

代码语言:javascript
复制
<div class="asp-checkbox">
    <span class="checkbox gaClick" data-category="companySignup" data-action="legitimateCompanyChecked" data-noninteraction="true">
        <input id="LegitimateCompanyAgreementCheckBox" type="checkbox" name="ctl00$MainContent$LegitimateCompanyAgreementCheckBox">
        <label for="LegitimateCompanyAgreementCheckBox">I am a legitimate estate sale company or auction company and have the documents necessary to conduct business in my state (if any).</label>
    </span>
    <label for="LegitimateCompanyAgreementCheckBox"></label>
    <span id="LegitimateCompanyAgreementCheckBox_requredCheckBoxValidor" controltovalidate="LegitimateCompanyAgreementCheckBox" style="color:Red;visibility:hidden;">* 
    </span>
</div>


<span class="checkbox gaClick" data-category="companySignup" data-action="agreeToTermsChecked" data-noninteraction="true">
    <input id="TermsOfServiceCheckBox" type="checkbox" name="ctl00$MainContent$TermsOfServiceCheckBox">
        <label for="TermsOfServiceCheckBox">I agree to EstateSales.NET's <a href="/terms-of-service" class="gaClick" data-category="companySignup" data-action="termsOfServiceClicked" target="_blank">Terms of Service</a>
and <a href="/privacy-policy" class="gaClick" data-category="companySignup" data-action="privacyPolicyClicked" target="_blank">Privacy Policy</a>.
    </label>
</span>
EN

回答 1

Stack Overflow用户

发布于 2021-10-18 19:37:05

在这种情况下,我们不需要单击标签标签。实际的复选框是输入标记。尝试将Xpath设置为::driver.FindElement(By.Id("LegitimateCompanyAgreementCheckBox")).Click();或driver.FindElement(By.XPath("//input@id='LegitimateCompanyAgreementCheckBox'")).Click();

以下是Wait:

代码语言:javascript
复制
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 120));
            WebElement element = wait.until(ExpectedConditions.ElementIsVisible(By.Id("LegitimateCompanyAgreementCheckBox")));
            element.Click();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69619835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档