2

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.

2
  • Seems like your XPath is correct but another element is preventing it from being clicked. Are you using a complex widget? Commented Jan 17, 2017 at 8:31
  • I can't understand the meaning of complex widget, but I'm using selenium webdriver for firefox in c#. Commented Jan 17, 2017 at 8:35

3 Answers 3

5

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();
Sign up to request clarification or add additional context in comments.

11 Comments

also, sometimes the element may be blocked by some menu or something overlapping it. This error also occurs in those scenarios.
@DhirajDas, Yes. Sometimes it happens. If this code doesn't solve current issue, I'll update it with solution considering your tips
I tried that. But the webdriver is still waiting after loading. I think my XPath is wrong.
@8berry, Did you try my XPath? In your code you use XPath in search by id- that's the problem. Also explain what do you mean webdriver is still waiting after loading?
@Andersson yes, but symptoms are same.
|
0

You can do something like

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

1 Comment

Thank you. But Element is not visible error occurred.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.