问题
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