如何使用C#Selenium从谷歌搜索中获取所有url(仅限第一页),然后从该列表中获取指定url的索引



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

我不确定你是如何知道你想搜索什么url的,但它就在这里。我添加了一个dictionary方法,以防你想存储索引和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

最新更新