简体   繁体   中英

select radio button in selenium c#

I'm new to XPath and CssSelector.

below is the target html source.

<input value="1" name="uji.model.611876.button" type="radio"></input>

611876 is a random number.

I tried with the code:

driver.FindElement(By.Id("//input[@value=\"1\"]")).Click();

and

driver.FindElement(By.Id("//input[@value='1']")).Click();

but the Unable to locate element error occurred.

I need help for that situation. Thank you for reading.

If you get ElementNotVisibleException try to wait some time until target input become visible:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[starts-with(@name, \"uji.model.\")][@type=\"radio\"]")));
element.Click();

你可以做类似的事情

driver.findElement(By.tagName("input")).Click();

You can try JavascriptExecuter to execute javascript code if it is causing problem -

IWebElement element= driver.FindElement(By.XPath("//input[@value=\"1\"]")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", element);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM