【问题标题】:How to get all urls from google search (first page only) then get the index of a specified url from that list, using C# Selenium?如何使用 C# Selenium 从谷歌搜索中获取所有 url(仅限第一页)然后从该列表中获取指定 url 的索引?
【发布时间】:2021-06-30 17:49:05
【问题描述】:

嘿,伙计们,在 C# 中使用 Selenium 和 NUnit 测试和 chrome,我如何从谷歌搜索的第一页获取列表中的所有主要 url(web url),然后获取指定 URL 的索引号那个列表?

【问题讨论】:

  • 只需单击 f12 并检查搜索结果以了解您应该在页面中搜索哪些元素。例如,单个搜索结果在具有“g”类的 div 中。关于在所有结果中搜索 - 据我所知,您可以在 selenium 中按类找到。它应该返回列表(但这只是假设)。然后只需使用 IndexOf() 或 foreach 循环来获取索引。

标签: c# selenium google-chrome selenium-chromedriver


【解决方案1】:

我不确定您是如何知道要搜索的网址的,但就是这样。我添加了一个字典方法,以防您想存储索引和 url 以供以后使用。

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Selenium
{
    public class Tests
    {
        public IWebDriver driver;


        [SetUp]
        public void Setup()
        {
            driver = new ChromeDriver(".");
        }

        [Test]
        public void TestUrlsIndex()
        {
            driver.Navigate().GoToUrl("https://google.com");

            //accept privacy
            driver.FindElement(By.XPath("//div[text()='I agree']")).Click();

            //type something and press Enter
            driver.FindElement(By.Name("q")).SendKeys("Retrieve Index" + Keys.Enter);

            //get all a elements 
            var a_webElements = driver.FindElements(By.XPath("//div[@class='g']/div/div/a"));


            //print the index for all
            for (int i = 0; i < a_webElements.Count; i++)
            {
                Console.WriteLine($"The index is {i} for the url {a_webElements[i].GetAttribute("href")}");
            }

            //when you know the url and want to return its index
            var index = ReturnIndexOfAnUrl(a_webElements, "https://stackoverflow.com/questions/41217310/get-index-of-a-row-of-a-pandas-dataframe-as-an-integer");



        }

        //when you want to search the url and return its index
        public int ReturnIndexOfAnUrl(IList<IWebElement> webElements, string searchHref)
        {
            return webElements.ToList().FindIndex(x => x.GetAttribute("href").Equals(searchHref));

        }

        //when you want to store the index and its corresponding url
        public Dictionary<int, string> ReturnIndexAsIndexAndUrlAsValue(IList<IWebElement> webElements)
        {
            var dictionary = new Dictionary<int, string>();

            for (int i = 0; i < webElements.Count; i++)
            {
                dictionary.Add(i, webElements[i].GetAttribute("href"));
            }

            return dictionary;
        }


        [TearDown]
        public void TearDown()
        {
            driver.Close();
        }

    }
}

你应该看到类似的东西

The index is 0 for the url https://stackoverflow.com/questions/41217310/get-index-of-a-row-of-a-pandas-dataframe-as-an-integer
The index is 1 for the url https://www.geeksforgeeks.org/how-to-get-rows-index-names-in-pandas-dataframe/
The index is 2 for the url https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html
The index is 3 for the url https://thispointer.com/python-find-indexes-of-an-element-in-pandas-dataframe/
The index is 4 for the url https://www.geeksforgeeks.org/how-to-get-rows-index-names-in-pandas-dataframe/
The index is 5 for the url https://www.geeksforgeeks.org/python-list-index/
The index is 6 for the url https://stackoverflow.com/questions/17426521/list-all-indexes-on-elasticsearch-server
The index is 7 for the url https://github.com/MikkelSchubert/paleomix/issues/36
The index is 8 for the url https://www.programiz.com/python-programming/methods/list/index
The index is 9 for the url https://www.sqlshack.com/gathering-sql-server-indexes-statistics-and-usage-information/
The index is 10 for the url https://dba.stackexchange.com/questions/141293/query-to-retrieve-index-details-for-a-particular-column-in-all-tables
The index is 11 for the url https://en.wikipedia.org/wiki/Database_index
The index is 12 for the url http://www.bcsea.bt/indexnumber
The index is 13 for the url https://aip.scitation.org/doi/10.1063/1.4752753

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2016-04-02
    • 1970-01-01
    • 2012-08-19
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多