最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Cycle through every link on a page with Webdriver - Stack Overflow

programmeradmin2浏览0评论

I've been asked to create a test which can cycle through all the links on a page with webdriver in C#.

I really don't where to begin with this, so sorry for not having any code to provide.

I'll need to somehow select all the links on the page and place them into an array or the like and them simply loop through them, click link, back to previous page, click next link etc.

I've been asked to create a test which can cycle through all the links on a page with webdriver in C#.

I really don't where to begin with this, so sorry for not having any code to provide.

I'll need to somehow select all the links on the page and place them into an array or the like and them simply loop through them, click link, back to previous page, click next link etc.

Share Improve this question asked Jul 31, 2014 at 12:09 VereonixVereonix 1,4346 gold badges29 silver badges57 bronze badges 2
  • You'll get a better response with a clearer question. Let's assume you are attempting what is monly called a navigation test. – Steve O Commented Jul 31, 2014 at 12:19
  • I cannot remend enough the very nice "Coypu" which is a wrapper around selenium/webdriver. It's way easier to use than "bare" webdriver. github./featurist/coypu – Gimly Commented Jul 31, 2014 at 12:19
Add a ment  | 

4 Answers 4

Reset to default 3

I assume it is something in the lines of:

        foreach (var item in _driver.FindElements(By.TagName("a")))
        {
            Trace.WriteLine(item.GetAttribute("href"));
        }

Edit:

Note this is written from head and approximate solution using NUnit.

[TestFixture]
class UnitTests
{
    [TestFixtureSetUp]
    public void FixtureSetup()
    {
        _driver = new ChromeDriver();
        _driver.Manage().Timeouts().ImplicitlyWait(Defaultamount);
        _wait = new WebDriverWait(_driver, Defaultamount);
    }
    [TestFixtureTearDown]
    public void FixtureTearDown()
    {
        _driver.Quit();
    }
    //...
}

Code would be something in the lines of :

    [TestCase("www.googel.")]
    public bool TestAllWebpageLinks(string url, Result = true)
    {
        _driver.Navigate().GoToUrl(url);
        var result = _driver.FindElements(By.TagName("a"))
            .Select(o=>o.GetAttribute("href"))
            .ToDictionary(o=>o,o=>TestPage(url,o));
        return result.All(o => o.Value);
    }
    public bool TestPage(string url, string link){
        try
        {
            _driver.Navigate().GoToUrl(url);
            _driver.FindElement(By.XPath("//a[@href='"+link+"']")).Click();     
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

Just as a note, Resharper plugin will simplify running unit tests a lot.

Please look here: How to browse a whole website using selenium?

Since you mentioned one page and not the whole site there are some straightforward ways.

populate an implementation of IWebElements with an xpath locator for //a...or get it by tagname as Margus mentions.

Then loop through this and click each IWebElement in the IWebElements list. Save the driver.Url in a list of items for review or output later with whatever else you want to capture from the page. You can validate here if you have known parameters to check for. Then execute a driver.Navigate().Back(). This will put you back in the previous spot to test the next element. Might also want to save the href as well as Margus suggests and pare that with the driver.Url on the other page.

Update based on ment

public class Links
{
    public string LinkName { get; set; }
    public string href { get; set; }
    public string newPageUrl { get; set; }
}

string newPageUrl = "";
string linkName = "";
string href = "";
List<Links> links = new List<Links>();
foreach (IWebElement item in _driver.FindElements(By.TagName("a")))
{
    href = item.GetAttribute("href");
    linkName = item.text();
    item.click();
    newPageUrl = driver.Url();
    links.Add(new Links
         {
             NewPageUrl  = newPageUrl,
             Href = href,
             LinkName = linkName
         });
    driver.Navigate().Back();
}

Then your List will be a list of that class which will have the specific properties for each link you want as well as the navigation with the new url. Then you can easily add something to this if you want to keep track of something else or test something else.

With Selenium-WebDriver you can simply call:

  • driver.findElements(By.TagName("a")) to return a collection of matching webelements.

Or if you want more options you could use a library like http://sizzlejs./ with webdriver which would let you call something like:

  • getElementsByTagName("a");

You could also use other external helpers like:

  • FluentAutomation to help with your test code

It depends on your page and how the links are generated.

This plete program might help you: The code is in Java

I am printing all the link Name.

import java.util.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TrailRuns 
{
    public static void main(String[] args) throws Exception
    {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.googl./");
        driver.manage().window().maximize();

        List<WebElement> objWEs = driver.findElements(By.tagName("a"));

        for(WebElement ele:objWEs)
            System.out.println(ele.getText());
    }
}
发布评论

评论列表(0)

  1. 暂无评论