简体   繁体   English

使用 python selenium 迭代谷歌搜索结果

[英]Iterating google search results using python selenium

I want to iterate clicking the google search results and copy menus of each site.我想迭代点击谷歌搜索结果并复制每个站点的菜单。 So far, i am through copying the menus and returning back to the results page but couldn't iterate clicking the results.For now, i would like to learn iterating search results alone but I'm stuck at stale element reference exception, i did see few other sources but no luck.到目前为止,我正在复制菜单并返回结果页面,但无法迭代单击结果。现在,我想单独学习迭代搜索结果,但我陷入了过时的元素引用异常,我做到了很少看到其他来源,但没有运气。

from selenium import webdriver
chrome_path = r"C:\Users\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('https://www.google.com?q=python#q=python')
weblinks = driver.find_elements_by_xpath("//div[@class='g']//a[not(@class)]");
for links in weblinks[0:9]:
    links.get_attribute("href")
    print(links.get_attribute("href"))
    links.click()
    driver.back()

StaleElementReferenceException means that elements you are referring to do not exist anymore. StaleElementReferenceException 意味着您所指的元素不再存在。 That usually happens when page is automatically redrawn.这通常发生在页面自动重绘时。 In your case, you change the page and navigate back, so elements would be redrawn 100%.在您的情况下,您更改页面并返回,因此元素将被重绘 100%。

Default solution is to search the list inside the loop every time.默认解决方案是每次搜索循环内的列表。

If you want to be sure that list is same every iteration, you need to add some additional check (compare texts, etc.)如果您想确保列表每次迭代都相同,则需要添加一些额外的检查(比较文本等)

If you use this code for scraping, probably you don't need back navigation.如果您使用此代码进行抓取,则可能不需要返回导航。 Just open every page directly with driver.get(href)直接用driver.get(href)打开每一页

Here you can find code example: How to open a link in new tab (chrome) using Selenium WebDriver?在这里您可以找到代码示例: 如何使用 Selenium WebDriver 在新选项卡(chrome)中打开链接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM