How do you click on a checkbox from a list of checkboxes via Selenium/Webdriver in Java?

后端 未结 8 1997
北海茫月
北海茫月 2020-12-01 22:59

I\'m using Selenium 2 (Webdriver) for automating tests on a webpage. However I wonder if there is way to check checkbox from the list of checkboxes using webdriver framework

相关标签:
8条回答
  • 2020-12-01 23:24

    If you already know the id of the checkbox, you can use this method to click select it:

    string checkboxXPath = "//input[contains(@id, 'lstCategory_0')]"
    IWebElement elementToClick = driver.FindElement(By.XPath(checkboxXPath));
    elementToClick.Click();
    

    Assuming that you have several checkboxes on the page with similar ids, you may need to change 'lstCategory_0' to something more specific.

    This is written in C#, but it shouldn't be difficult to adapt to other languages. Also, if you edit your post with some more information, I can fine-tune this example better.

    Let me know if this works!


    I've visited the site and successfully interacted with the checkboxes in the dropdown widget using this code:

    /** Set XPath Variables **/
    string dropdownWidgetXPath = "//span[contains(@id, 'selInd')]";
    string checkboxXPath = "//input[contains(@id, 'selInd')]";
    
    /** Navigate to the page **/
    driver.Navigate().GoToUrl("http://www.jobserve.com/us/en/Job-Search/");
    
    /** Click the dropdown widget **/
    IWebElement dropdownWidgetElement = driver.FindElement(By.XPath(dropdownWidgetXPath));
    dropdownWidgetElement.Click();
    
    /** Identify all checkboxes present **/
    var allCheckboxes = driver.FindElements(By.XPath(checkboxXPath));
    
    /** Click each checkbox and wait so that results are visible **/
    foreach(IWebElement checkbox in allCheckboxes)
    {
         checkbox.Click();
         System.Threading.Thread.Sleep(500);
    }
    
    0 讨论(0)
  • 2020-12-01 23:35

    By id of the checkbox you could use following code:

    IWebElement elementToClick = driver.FindElement(By.ID(ctl00_ContentPlaceHolder1_Add_lstCategory_0));
    elementToClick.Click();
    

    If you don't know id then use below code by xpath:

    String checkbox = "//input[@type='checkbox']"
    IWebElement elementToClick = driver.FindElement(By.XPath(checkbox ));
    elementToClick.Click();
    
    0 讨论(0)
  • 2020-12-01 23:35

    This is how I check and uncheck all my boxes, it has to have an Id or class.

    Id example:

    driver.FindElement(By.Id("someid")).click();
    

    ClassName examaple:

    driver.FindElement(By.ClassName("someid")).click();
    

    Its short, its sweet and more importantly it works.

    0 讨论(0)
  • 2020-12-01 23:39

    The code in selenium is simple:

    new WebDriverWait(driver, TimeSpan.FromSeconds(timeToHoldOn)).Until(ExpectedConditions.ElementExists((By.ClassName("ckb"))));
    IWebElement dropdownWidgetElement = driver.FindElement(By.ClassName("ckb"));
    dropdownWidgetElement.Click();
    Thread.Sleep(1000);
    var allCheckboxes = driver.FindElements(By.ClassName("ckb"));
    foreach (IWebElement checkbox in allCheckboxes) {
      checkbox.Click();
      System.Threading.Thread.Sleep(250);
    }
    
    0 讨论(0)
  • 2020-12-01 23:42

    In Selenium webdriver you can do it like this :

    All the check-boxes must be having some unique identifier then you can simply find it out by Id If they dont have a unique id (This is what I encountered while testing a web application) then it must be having some title and name attribute (or some other attribute).

    Then you can try this :

    driver = new FirefoxDriver(); 
    driver.findElement(By.xpath("//input[@name='mycheckboxgroup' and @title='movies']")).click();    
    driver.findElement(By.xpath("//input[@name='mycheckboxgroup' and @title='songs']")).click();
    
    0 讨论(0)
  • 2020-12-01 23:42

    Java, Clicking on multiple checkboxes at a time using the loop.

    **Sample Xpath :**
    
    CheckBox1 Xpath : //input[@class='mycheck' and @id='1']
    CheckBox2 Xpath : //input[@class='mycheck' and @id='2']
    

    Get all the elements checkbox using findelements:

    List WebElement ele = driver.findElements(By.xpath("//input[@class='mycheck']"));

    Make the Xpath as string by leaving the ID and assign the ID as i.

    for(int i=1; i<=ele.size(); i++) { driver.findElement(By.xpath("//input[@class='mycheck' and @id='" +  + i + "']")).click(); }
    

    i gets the value for every loop and the xpath matches the checkbox and click's it.

    0 讨论(0)
提交回复
热议问题