【问题标题】:How to deal with checkbox using selenium webdriver?如何使用 selenium webdriver 处理复选框?
【发布时间】:2023-04-11 03:05:01
【问题描述】:

我想抓取一个带有没有 id 的复选框的页面,它们具有相同的名称,只是值不同。

<div class="mvNavLk">
    <form class="jsExpSCCategories" method="post" name="ExpressSCCategories" action="actionTest.html">
        <ul class="mvSrcLk">
            <li>
                <label class="mvNavSel mvNavLvl1">
                    First
                    <input type="checkbox" value="firstValue" name="selectedNavigationCategoryPath">
                </label>
            </li>
            <li>
                <label class="mvNavSel mvNavLvl1">
                    Second
                    <input type="checkbox" value="secondValue" name="selectedNavigationCategoryPath">
                </label>
            </li>
        </ul>
    </form>
</div>

【问题讨论】:

  • 你想设置什么?你想访问什么元素?
  • @theRoot 我想在复选框中设置值,例如“firstValue”,
  • 您要设置值或单击值为firstValue的复选框
  • 我想点击值为firstValue的复选框,
  • 看来你用错了方法。 select=driver.find_element_by_tag_name("selectedNavigationCategoryPath") 来自 HTML sn-p,看来您应该使用 'find_element_by_name' 方法。

标签: selenium selenium-webdriver web-scraping web-crawler


【解决方案1】:

使用下面的代码:

driver.find_element_by_css_selector(".mvSrcLk>li:nth-child(1)>label.mvNavSel.mvNavLvl1").click();

希望这会奏效。

【讨论】:

  • 非常感谢,它有效,所以我们不需要值?
【解决方案2】:

希望这可行-

 driver.FindElement(By.XPath(".//label[contains(text(),'First')]/input")).SendKeys("test");

【讨论】:

    【解决方案3】:

    您好,请按照下面的方式进行操作注意这个例子是在java中

    // take check boxes with same name inside the list 
    List<WebElement> myCheckBox = driver.findElements(By.name("selectedNavigationCategoryPath"));
    
    // now on the basis of index call click
    
    myCheckBox.get(0).click();  // for the first check box
    Thread.sleep(2000);
    myCheckBox.get(1).click();  // for the second check box
    

    或者如果你想根据价值进行选择,那么

    driver.findElement(By.xpath("//*[@value='firstValue']")).click();  // for the 1st one
    Thread.sleep(2000);
    driver.findElement(By.xpath("//*[@value='secondValue']")).click();  // for the 2st one
    

    更新

    WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@value='firstValue']")));
    driver.findElement(By.xpath("//*[@value='firstValue']")).click();  // for the 1st one
    

    希望对你有帮助

    【讨论】:

    • 我没有复选框的数量,
    • 这就是为什么我首先将它放入列表中,将列表中的所有内容 java 为每个值提供一个索引值,并且在 java 中索引从 0 开始,因此第一个复选框索引为 0,第二个复选框索引为 1 以此类推
    • 我收到此错误:ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互
    • 嗨,我已经更新了我的答案,请看一下我已经使用 webdriverwait 告诉 web 驱动程序在抛出任何错误之前等待元素可见性 30 秒
    • 我用网站名称和我的代码更新了我的问题,包括你的建议,它不起作用
    【解决方案4】:
    driver.findElement(By.name("selectedNavigationCategoryPath")).click();
    

    【讨论】:

      【解决方案5】:
      Below Code is in C#, Collects all the Checkboxes with the name specified. Then Iterates through each ckeckbox, gets the value attribute, if the attribute is equal to your specified check box, then clicks it and comes out of the loop. Hope this should work.
      IList<IWebElement> myCheckBoxs =    driver.FindElements(By.name("selectedNavigationCategoryPath"));
      
      Foreach(IWebElement chkBx in myCheckBoxs)
      {
         if(chkBx.GetAttribute("Value")=="Your Desired value")
         {
           chkBx.Click();
           break;
         }
      }
      

      `

      【讨论】:

        【解决方案6】:

        使用这个 CSS 定位器。

        [name='selectedNavigationCategoryPath'][value='firstValue']
        

        【讨论】:

          猜你喜欢
          • 2011-11-13
          • 2014-05-13
          • 2017-06-09
          • 2016-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-19
          • 2014-06-06
          相关资源
          最近更新 更多