i have my html code looks like this:
As you have the link under a tag so selenium must work with below code.
You xpath will be .//*[@id='food-search-all']/ul/li
(assuming that div id is unique .You can take help with firebug for generate xpath )
IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal));
string CellVal = TargetElement.Text.ToString();
TargetElement = driver.FindElement(By.LinkText(CellVal));
TargetElement.Click();
sometimes sigle use of .Click() not work i will suggest add one more TargetElement.Click(); if your first try not work.
Small error in your code, you're missing '' around the text "+linktext+"
:
driver.findElement(By.xpath("/table[contains(@class,'results ib-list')]/tbody/tr/td[@class = 'name']/a[contains(text(),'"+linktext+"')]")).click();
NoSuchElementException
usually means the element is in a frame
, or slow to load. Selenium only interacts with elements in the current frame. Any element within a child frame
cannot be interacted with until you switch into that frame
. You can switch by using switchTo().frame()
:
driver.switchTo().frame(ARG);
The arguments for frame()
are
frame
webelement
rerference of the frame
When done in the iframe
, use the following to exit back to the top of the document:
driver.switchTo().defaultContent();