3

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?

4 Answers 4

7

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

Comments

0

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

2 Comments

Actually, it does. The problem I am facing is that within the loop, selenium.click(...) should be available as I saw in many examples. However, it is not there. Selenium does not list click() as an available method that I can use. The ...ToList() actually fills the links list with all the available links on the page and they are all correct.
maybe you got something wrong in your code. I'm java programmer, from my point of view you define links as a method? I would possibly write it like in my above edited answer
0

Can you try casting he link to IWebELement in your foreach loop like:

 foreach(IWebELelent link in links) 
{
------
-----
}

Comments

0
driver.FindElement(By.Xpath("")).Click();

or

driver.FindElement(By.Xpath("")).SendKeys(Open.QA.Selenium.Keys.Enter);

Either way is possible

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.