C# 有没有一种方法(在c语言中)可以迭代浏览网站上的文章元素';什么是HTML?

C# 有没有一种方法(在c语言中)可以迭代浏览网站上的文章元素';什么是HTML?,c#,html,selenium,selenium-webdriver,C#,Html,Selenium,Selenium Webdriver,我试图遍历页面上的元素,它们都在html下分类 简而言之,我需要在一个页面上迭代一系列元素(上面列出了HTML代码),并使用关键字对照innerHTML:Supreme®/Honda®/Fox®Racing Puffy拉链夹克测试关键字。关键词像:本田、福克斯和蓬松 如果一个元素匹配两个或多个关键字,那么它会单击该元素 do { driver.Navigate().Refresh(); 尝试 { driver.FindElement(By.LinkText(“繁殖圆领”); element

我试图遍历页面上的元素,它们都在
html下分类

简而言之,我需要在一个页面上迭代一系列元素(上面列出了HTML代码),并使用关键字对照innerHTML:Supreme®/Honda®/Fox®Racing Puffy拉链夹克测试关键字。关键词像:本田、福克斯和蓬松

如果一个元素匹配两个或多个关键字,那么它会单击该元素

do
{
driver.Navigate().Refresh();
尝试
{
driver.FindElement(By.LinkText(“繁殖圆领”);
elementFound=true;
}捕获(NoTouchElementException错误)
{
WriteLine(“没有找到这样的元素!”);
}
}while(elementFound==false);
这是我当前的代码。它只在页面中查找LinkText元素。这是不可行的,因为它需要一个特定的字符串;LinkText选项不能使用关键字。此外,它是一个真/假条件语句。我需要实现迭代。

类似这样的东西?:

    var driveroptions = new ChromeOptions();
                driveroptions.AddUserProfilePreference("disable-popup-blocking", "true");

                using (IWebDriver driver = new ChromeDriver(this.SeleniumDriverPath, driveroptions))
                {
                    try
                    {
                        driver.Navigate().GoToUrl("http://www.google.com/");
                        IWebElement query = driver.FindElement(By.Name("q"));
                        query.SendKeys("link");
                        query.Submit();
                        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                        //
                        var listElement = driver.FindElements(OpenQA.Selenium.By.XPath(".//*[@id='search']//div[@class='g']"));
                        foreach( var e in listElement)
                        {
                            //include ctrl+click:
                            //var action = new OpenQA.Selenium.Interactions.Actions(driver);
                            //action.KeyDown(Keys.Control).Build().Perform();
                            //or click:
                            if ("Supreme®/Honda®/Fox® Racing Puffy Zip Up Jacket".Contains(e.Text))
                            {
                                e.Click();
                            }
                        }
                        //
                    }
                    driver.Quit();
                }
基本上,您可以通过XPath查找元素:

    driver.FindElements(OpenQA.Selenium.By.XPath(".//*article//a[@class='name-link']"));
或按类名:

    driver.FindElements(OpenQA.Selenium.By.ClassName("name-link"))
迭代,并根据条件执行单击?

类似的操作?:

    var driveroptions = new ChromeOptions();
                driveroptions.AddUserProfilePreference("disable-popup-blocking", "true");

                using (IWebDriver driver = new ChromeDriver(this.SeleniumDriverPath, driveroptions))
                {
                    try
                    {
                        driver.Navigate().GoToUrl("http://www.google.com/");
                        IWebElement query = driver.FindElement(By.Name("q"));
                        query.SendKeys("link");
                        query.Submit();
                        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                        //
                        var listElement = driver.FindElements(OpenQA.Selenium.By.XPath(".//*[@id='search']//div[@class='g']"));
                        foreach( var e in listElement)
                        {
                            //include ctrl+click:
                            //var action = new OpenQA.Selenium.Interactions.Actions(driver);
                            //action.KeyDown(Keys.Control).Build().Perform();
                            //or click:
                            if ("Supreme®/Honda®/Fox® Racing Puffy Zip Up Jacket".Contains(e.Text))
                            {
                                e.Click();
                            }
                        }
                        //
                    }
                    driver.Quit();
                }
基本上,您可以通过XPath查找元素:

    driver.FindElements(OpenQA.Selenium.By.XPath(".//*article//a[@class='name-link']"));
或按类名:

    driver.FindElements(OpenQA.Selenium.By.ClassName("name-link"))
迭代,并根据条件执行单击?

1)您将获得所有文章中所有链接的列表

  protected IList<IWebElement> FindNestedElements()
            {
             return FindElement(By.XPath("your xpath for 
     class="name-link" element")).FindElements(By.XPath("your Xpath for article elements"));
            }
1) 您正在获取所有文章中所有链接的列表

  protected IList<IWebElement> FindNestedElements()
            {
             return FindElement(By.XPath("your xpath for 
     class="name-link" element")).FindElements(By.XPath("your Xpath for article elements"));
            }

我会将关键字列表存储在字符串数组中。然后在每个
文章
标签下抓取每个
标签(该标签包含产品名称,例如“Supreme®/Honda®/Fox®Racing Poffy
然后使用LINQ计算该字符串中的关键字数。如果计数>=2,则单击该元素。代码如下

string[] keywords = { "Honda", "Fox", "Puffy" };
foreach (IWebElement link in _driver.FindElements(By.CssSelector("article a.name-link")))
{
    if (keywords.Count(w => link.Text.Contains(w)) >= 2)
    {
        link.Click();
        break;
    }
}

我会将关键字列表存储在字符串数组中。然后在每个
文章
标签下抓取每个
标签(该标签包含产品名称,例如“Supreme®/Honda®/Fox®Racing Poffy
然后使用LINQ计算该字符串中的关键字数。如果计数>=2,则单击该元素。代码如下

string[] keywords = { "Honda", "Fox", "Puffy" };
foreach (IWebElement link in _driver.FindElements(By.CssSelector("article a.name-link")))
{
    if (keywords.Count(w => link.Text.Contains(w)) >= 2)
    {
        link.Click();
        break;
    }
}

我不完全清楚你在这里想要实现什么。因此,您希望在页面上获得
文章
元素的列表,对它们进行迭代,然后检查每篇文章中的
a
标记中的一些文本,如果文本匹配,请单击某个内容?我不完全清楚您在这里试图实现什么。因此,您希望在页面上获得
文章
元素的列表,对它们进行迭代,然后检查每篇文章中的
a
标记中的一些文本,如果文本匹配,请单击一些内容?非常感谢!我刚刚添加了一个
c#try{]catch{}
block来处理StaleElementReferenceException异常。除此之外,它工作得非常好…谢谢!我以前应该对此发表评论,因为我知道这可能会成为一个问题。问题是,一旦单击链接,您将被导航到一个新页面,因此所有引用都将过时。如果您只想要单击它的第一个链接,请在
链接之后放置一个
break;
。单击()
。查看更新。这将导致
foreach
循环终止并避免过时的异常。非常感谢!我刚刚添加了一个
c 35; try{]catch{}
block来处理StaleElementReferenceException异常。除此之外,它工作得非常好…谢谢!我以前应该对此发表评论,因为我知道这可能会成为一个问题。问题是,一旦单击链接,您将被导航到一个新页面,因此所有引用都将过时。如果您只想要单击它的第一个链接,请在
链接之后放置一个
中断;
。单击()
。查看更新。这将导致
foreach
循环终止,并避免过时的异常。