Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Selenium WebDriver w/C迭代链接#_C#_.net_Selenium Webdriver_Visual Studio Code - Fatal编程技术网

C# 使用Selenium WebDriver w/C迭代链接#

C# 使用Selenium WebDriver w/C迭代链接#,c#,.net,selenium-webdriver,visual-studio-code,C#,.net,Selenium Webdriver,Visual Studio Code,我有一个项目,使用Selenium WebDriver和C#在网页上点击特定链接,我正在努力找到一种干净的方法,用一个数组来遍历这些链接,该数组可以解释特定的情况 我了解如何执行基本的driver.FindElement(By.XPath(“”)) 但我不确定如何创建一个WebElement数组来为foreach语句提供数据,该语句将在特定的div类中按.TagName(“a”)进行搜索,而无需提取页面上的每个链接 网站标题的外观示例: 我刚才使用findelement的基本示例: usin

我有一个项目,使用Selenium WebDriver和C#在网页上点击特定链接,我正在努力找到一种干净的方法,用一个数组来遍历这些链接,该数组可以解释特定的情况

我了解如何执行基本的driver.FindElement(By.XPath(“”))

但我不确定如何创建一个WebElement数组来为foreach语句提供数据,该语句将在特定的div类中按.TagName(“a”)进行搜索,而无需提取页面上的每个链接

网站标题的外观示例:


我刚才使用findelement的基本示例:

using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        using (IWebDriver driver = new ChromeDriver("C:\\"))
        {
            driver.Navigate().GoToUrl("xxx");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);

            driver.FindElement(By.XPath("//a[text()=\"xxx\"]")).Click();
            // TODO: Figure out how to assert route change occurred

            driver.FindElement(By.XPath("//a[text()=\"xxx\"]")).Click();
            driver.Navigate().Back();

            driver.FindElement(By.XPath("//a[text()=\"xxx/I\")]")).Click();
            driver.Navigate().Back();

            Console.WriteLine("This is what happens when you don't know how to make an array.");
            driver.Quit();
        }    
    }
}
因此,总结一下:


需要帮助找到一种方法来创建一个数组,该数组可以在循环中找到要单击的特定链接,因为在搜索了一段时间后,这似乎是最简单的解决方案。如果有更好的建议,我愿意接受。对于C#/Selenium来说,这是一个全新的概念。

通过确定链接的xpath,您可以获得所有链接

var links = myWebDriver.FindElements(By.XPath("/html/body/div[2]/header/div[1]/div/div/a"));

//loop through all header links
for (int i = 0; i < links.Count; i++)
{
    //reassignment because of reloading the page after each navigation
    links = myWebDriver.FindElements(By.XPath("/html/body/div[2]/header/div[1]/div/div/a"));
    myWebDriver.Navigate().GoToUrl(links[i].GetAttribute("href"));
    //or you can click
    //links[i].Click();
    myWebDriver.Navigate().Back();
}
var links=myWebDriver.FindElements(By.XPath(“/html/body/div[2]/header/div[1]/div/div/a”);
//循环通过所有标题链接
for(int i=0;i
接下来的链接如图所示:

var links2 = myWebDriver.FindElements(By.XPath("/html/body/div[2]/header/div[2]/div/ul/li/a"));
for (int i = 0; i < links2.Count; i++)
{
    links2 = myWebDriver.FindElements(By.XPath("/html/body/div[2]/header/div[2]/div/ul/li/a"));
    myWebDriver.Navigate().GoToUrl(links2[i].GetAttribute("href"));
    //or you can click        
    //links2[i].Click();
    myWebDriver.Navigate().Back();
}
var links2=myWebDriver.FindElements(By.XPath(“/html/body/div[2]/header/div[2]/div/ul/li/a”);
对于(int i=0;i
您可以使用selenium提供的
FindElements
方法获取列表中的所有元素。然后,您可以使用
GetAttribute()
方法获取
href
并导航到它们中的每一个

IReadOnlyCollection<IWebElement> elementList = Driver.FindElements(By.XPath(".//div[@class='headerMenu']"));
foreach (IWebElement item in t){
Driver.Navigate.GoToUrl(item.GetAttribute("href"));
Driver.Navigate().Back();
}            
IReadOnlyCollection elementList=Driver.FindElements(By.XPath(“.//div[@class='headerMenu']”);
foreach(t中的IWebElement项){
Driver.Navigate.gotour(item.GetAttribute(“href”);
Driver.Navigate().Back();
}