問題描述
我正在嘗試選擇一個單選按鈕和輸入元素,它有一個組的 id
和 In_Group
的值.有 4 個不同的單選按鈕具有相同的 id 但不同的值,因此我正在嘗試選擇我正在尋找的正確的單選按鈕.
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">
我嘗試過這樣的事情:
driver.FindElement(By.XPath("http://*[contains(@id='group' and @value='In_Group')]"))
但是找不到元素,誰能幫幫我
But the element is not found could someone help me out
推薦答案
要定位元素,您可以使用以下任一方法 定位器策略:
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("http://input[@id='group' and @value='In_Group']"));
但是,由于它是一個 <input>
元素,并且您可能會在理想情況下與之交互,因此您必須誘導 WebDriverWait 用于所需的 ElementToBeClickable()
并且您可以使用以下任一 Locator Strategies:
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("http://input[@id='group' and @value='In_Group'][@class='custom-radio' and @name='group']"))).Click();
這篇關于如何使用 Selenium 和 C# 通過元素 ID 屬性單擊單選按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!