[英]How to fetch google search results links using selenium and python
I am trying to fetch all the search results link and print it.我正在尝试获取所有搜索结果链接并打印出来。 The problem I found is I am not able to iterate using the xpath of the individual search results because我发现的问题是我无法使用单个搜索结果的 xpath 进行迭代,因为
elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a') elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a')
is one result and the next result's link is是一个结果,下一个结果的链接是
elems = driver.find_elements_by_xpath('div[3]/div/div[1]/a') elems = driver.find_elements_by_xpath('div[3]/div/div[1]/a')
The index value of div is what iterates throughout the results. div 的索引值是在整个结果中迭代的值。 Currently I am able to fetch only one link with the index value of div.目前我只能用 div 的索引值获取一个链接。 I want to fetch all of the results and append it to a list for further processing.我想获取所有结果并将 append 提取到列表中以进行进一步处理。 Help me out on this one帮我解决这个问题
driver = webdriver.Chrome()
driver.get('http://www.google.com')
search_input = driver.find_element_by_name('q')
search_input.send_keys('stackoverflow')
search_input.send_keys(Keys.ENTER)
results = []
elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a')
for elem in elems:
href = elem.get_attribute('href')
print(href)
results.append(href)
Create a counter i and place it in your xpath.创建一个计数器 i 并将其放在您的 xpath 中。 Suppose i is integer counter before using it in xpath convert it into string using假设我是 integer 计数器,然后在 xpath 中使用将其转换为字符串
string= str(i)
elems = driver.find_elements_by_xpath("div['"+ string +"']/div/div[1]/a")
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class GooglesearchTests {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver83\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Testing");
Thread.sleep(Long.parseLong("1000"));
List<WebElement> LIST = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
System.out.println(LIST.size());
for (int i = 0; i < LIST.size(); i++)
{
//System.out.println(LIST.get(i).getText());
if (LIST.get(i).getText().contains("testing types"))
{
LIST.get(i).click();
break;
}
}
}
}
This is java code for google search results
every day google page elements are changing ( What i am doing is open google and type testing and store all results in list and pick "Testing Type" from list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.