Reputation: 51
I am just starting on Selenium. I am trying to invoke click actions upon links on a web page, but for some reason, selenium.click() event is not even showing on the intellisense! inside the foreach loop. Below is partial code of what I am trying to do.
IWebDriver driver;
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
List<IWebElement> links = new List<IWebElement>();
links= driver.FindElements(By.TagName("a")).ToList();
//driver.FindElement(By.LinkText("YouTube")).Click();
foreach (var link in links)
{
OpenQA.Selenium....;
..
}
Please note that the click works fine in the commented line above the foreach loop. Am I missing a reference?
Upvotes: 3
Views: 31826
Reputation: 497
driver.FindElement(By.Xpath("")).Click();
or
driver.FindElement(By.Xpath("")).SendKeys(Open.QA.Selenium.Keys.Enter);
Either way is possible
Upvotes: 0
Reputation: 32845
Why do you expect selenium.Click();
to show up? From the code you provided, looks like you are using WebDriver, not Selenium RC or WebDriverBackSelenium. You probably should consider using something like link.Click();
.
Here is what I do using WebDriver, which works fine for me.
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
// find directly, note it's not in the <a> but in <span>
// driver.FindElement(By.XPath("//span[text()='YouTube']")).Click();
// your logic with LINQ
IList<IWebElement> links = driver.FindElements(By.TagName("a"));
links.First(element => element.Text == "YouTube").Click();
// your logic with traditional foreach loop
foreach (var link in links) {
if (link.Text == "YouTube") {
link.Click();
break;
}
}
driver.Quit();
Upvotes: 7
Reputation: 1
Can you try casting he link to IWebELement in your foreach loop like:
foreach(IWebELelent link in links)
{
------
-----
}
Upvotes: 0
Reputation: 3241
I guess the By
method is not finding your TagName. Try the By.LinkText("a")
instead:
links= driver.FindElements(By.LinkText("a")).ToList();
Or try other By
methods (id,className,...)
€:
List<IWebElement> links = new List<IWebElement>();
links.add(driver.FindElements(By.TagName("a")));
//driver.FindElement(By.LinkText("YouTube")).Click();
links.get(0).click();
Upvotes: 0