hazra
hazra

Reputation: 430

How to navigate through webpage links and back again using Selenium?

I am trying to locate all hyperlinks on a webpage of which the link text is also in the list, footerNames. I want to click the link, wait (will put a verification check in later), navigate back and then click the next link in the allLinksHrefs list (which should contain all the necessary webpage link elements). At the moment I am able to locate the first link, click it, navigate back but then the test fails. I am using Selenium WebDriver with C#. Thanks in advance.

    public void TestFooterPageLinks()
    {
        List<IWebElement> allLinksHrefs = new List<IWebElement>();
        List<String> allLinksText = new List<String>();
        String currentUrl = Browser.Url;

        List<String> footerNames = new List<String>();
        footerNames.Add("About");
        footerNames.Add("Press");
        footerNames.Add("Safety");
        footerNames.Add("Privacy");
        footerNames.Add("Help");
        footerNames.Add("Terms");
        foreach (IWebElement link in Browser.FindElements(By.TagName("a")))
        {
            if (footerNames.Contains(link.Text))
            {
                if (allLinksHrefs.Contains(link)) {
                    continue;
                }
                else {
                    allLinksHrefs.Add(link);
                    Console.WriteLine(link);
                }
            }
        }
        foreach (IWebElement pageLink in allLinksHrefs)
        {
            Console.WriteLine(pageLink);
            pageLink.Click();
            Console.WriteLine(Browser.Title);
            Browser.Wait(3);
            Browser.Back();
        }
    }
}

Upvotes: 2

Views: 10783

Answers (2)

DSL
DSL

Reputation: 116

Try in this way it solves your problem "after navigating back the elements found previously will be expired. Hence we need to update the code to re-find the elements after navigate back"

driver.navigate().to("http://www.example.com/");
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
for(int i=0; i<links.size(); i++) {
    // Print the link text
    System.out.println(links.get(i).getText());
    // Print the href's
    System.out.println(links.get(i).getAttribute("href"));
    links.get(i).click();
    System.out.println(driver.getTitle());
    System.out.println(driver.getCurrentUrl());
    driver.navigate().back();
    /* after navigating back the elements found previously will be expired. 
       Hence we need to update the code to refind the elements after navigate back.
       so again we write the links = driver.findElements(By.tagName("a")); */
    links = driver.findElements(By.tagName("a"));
}

Upvotes: 2

bcar
bcar

Reputation: 815

Here's how i'd possibly achieve this:

List<WebElement> links = driver.findElements(By.cssSelector('a'));

The above gets you every '

Then I could see a couple different approaches, i'll outline one trivial example.

Iterator<WebElement> iter = links.iterator();

Set up the iterator object

while (iter.hasNext()) {
    WebElement we = iter.next();
    visitPage(we);
}

Then have the supporting visit page function

public void visitPage(WebElement link) {
    try {
        link.click()
        Console.WriteLine("Visited Page: " + driver.getTitle() + " at url: " + link.text()); //for page title
    } catch (Exception e) {
        //do something clever with error handling
        e.printStackTrace();
    } 
    // navigate back
    driver.manage.navigate.back();  //go back to previous page (maybe don't even care about this)
}
  • This is untested.

Upvotes: -1

Related Questions