如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接

How to click a random link from google search results through Selenium and Python(如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接)

本文介绍了如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前使用 Selenium+ Python 和这段代码:

Currently using Selenium+ Python and this code:

browser.get('http://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("how to search the internet")
search.send_keys(Keys.RETURN) # hit return after you enter search text
time.sleep(5) # sleep for 5 seconds so you can see the results
browser.execute_script("window.scrollTo(0, 873)") 
time.sleep(2)
browser.find_element(By.XPATH, '(//h3)[3]/a').click()`

作为我的代码.

会发生什么:转到 Google 并输入单词并点击搜索.向下滚动一点,然后点击第一个链接

What Happens: Goes to Google and types in the words and hits search. Scrolls down a bit and then it clicks the first link

我想要什么:我希望能够点击搜索结果页面中的随机链接

What I want: I want to be able to click a random link from the search result page

如何做到这一点?

这就是我所说的不必要时的意思:不必要的1:https://imgur.com/a/70qz89x

This is what I meant when I said unnecessary: Unnecessary 1: https://imgur.com/a/70qz89x

不必要的2:https://imgur.com/a/8WWvcnC

这些是我想要的唯一结果:这个:https://imgur.com/a/eTatFV9

These are the only results I want: This: https://imgur.com/a/eTatFV9

推荐答案

根据您的问题 click() 来自谷歌搜索结果的随机链接,根据您的 code trial 如果您调用 window.scrollTo(0, 873) 然后调用 click(),如下所示:

As per your question to click() on a random link from google search results, as per your code trial if you invoke window.scrollTo(0, 873) and then invoke click() as in:

find_element(By.XPATH, '(//h3)[3]/a').click()`

Selenium 仍会尝试在第一次匹配时尝试 click(),这可能不是您想要的用例.

Selenium will still try to attempt click() on the first match, which may not be your desired usecase.

为了 click() 来自谷歌搜索结果的随机链接,您可以从 搜索结果 中创建一个 List,然后生成一个随机数并通过一个index调用click(),如下:

Inorder to click() on a random link from google search results, you can create a List out of the search results and then generate a Random Number and invoke click() through an index as follows:

  • 代码块:

  • Code Block:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys 
from random import randint

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
browser.get('http://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("selenium")
search.send_keys(Keys.RETURN)
my_search_list = WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//h3[@class='r']/a[not(ancestor::div[@class='xIleA'])]")))
myRandomNumber = randint(0, len(my_search_list))
print(myRandomNumber)
my_search_list[myRandomNumber].click()

  • 控制台输出:

  • Console Output:

    4
    

  • 这篇关于如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接

    基础教程推荐