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

后端 未结 8 2033
北海茫月
北海茫月 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);
    }
    

提交回复
热议问题