How to click on the radio button through the element ID attribute using Selenium and C#

倾然丶 夕夏残阳落幕 提交于 2021-01-28 09:50:14

问题


I am trying to select a radio button and input element, it has an id of group and value of In_Group. There are 4 different radio buttons with the same id but different values hence I am trying to select the correct one i am looking for.

<input class="custom-radio" id="group" name="group" type="radio" value="In_Group">

I tried something like this:

driver.FindElement(By.XPath("//*[contains(@id='group' and @value='In_Group')]"))

But the element is not found could someone help me out


回答1:


To locate the element you can use either of the following Locator Strategies:

  • CssSelector:

    driver.FindElement(By.CssSelector("input#group[value='In_Group']"));
    
  • XPath:

    driver.FindElement(By.XPath("//input[@id='group' and @value='In_Group']"));
    

However, as it is a <input> element and possibly you will interact with it ideally you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.custom-radio#group[value='In_Group'][name='group']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='group' and @value='In_Group'][@class='custom-radio' and @name='group']"))).Click();
    


来源:https://stackoverflow.com/questions/62451267/how-to-click-on-the-radio-button-through-the-element-id-attribute-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!