• <bdo id='YzFOB'></bdo><ul id='YzFOB'></ul>
    <legend id='YzFOB'><style id='YzFOB'><dir id='YzFOB'><q id='YzFOB'></q></dir></style></legend>

        <i id='YzFOB'><tr id='YzFOB'><dt id='YzFOB'><q id='YzFOB'><span id='YzFOB'><b id='YzFOB'><form id='YzFOB'><ins id='YzFOB'></ins><ul id='YzFOB'></ul><sub id='YzFOB'></sub></form><legend id='YzFOB'></legend><bdo id='YzFOB'><pre id='YzFOB'><center id='YzFOB'></center></pre></bdo></b><th id='YzFOB'></th></span></q></dt></tr></i><div id='YzFOB'><tfoot id='YzFOB'></tfoot><dl id='YzFOB'><fieldset id='YzFOB'></fieldset></dl></div>
        <tfoot id='YzFOB'></tfoot>

        <small id='YzFOB'></small><noframes id='YzFOB'>

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

        How to click a random link from google search results through Selenium and Python(如何通过 Selenium 和 Python 从谷歌搜索结果中单击随机链接)
        <legend id='vJmAp'><style id='vJmAp'><dir id='vJmAp'><q id='vJmAp'></q></dir></style></legend>
          <bdo id='vJmAp'></bdo><ul id='vJmAp'></ul>

          <i id='vJmAp'><tr id='vJmAp'><dt id='vJmAp'><q id='vJmAp'><span id='vJmAp'><b id='vJmAp'><form id='vJmAp'><ins id='vJmAp'></ins><ul id='vJmAp'></ul><sub id='vJmAp'></sub></form><legend id='vJmAp'></legend><bdo id='vJmAp'><pre id='vJmAp'><center id='vJmAp'></center></pre></bdo></b><th id='vJmAp'></th></span></q></dt></tr></i><div id='vJmAp'><tfoot id='vJmAp'></tfoot><dl id='vJmAp'><fieldset id='vJmAp'></fieldset></dl></div>

            <small id='vJmAp'></small><noframes id='vJmAp'>

              <tbody id='vJmAp'></tbody>

              <tfoot id='vJmAp'></tfoot>
                  本文介绍了如何通过 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()
                  

                1. 控制台输出:

                2. Console Output:

                  4
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                    <tbody id='Vi1mY'></tbody>

                    <tfoot id='Vi1mY'></tfoot>

                      <legend id='Vi1mY'><style id='Vi1mY'><dir id='Vi1mY'><q id='Vi1mY'></q></dir></style></legend>
                        1. <i id='Vi1mY'><tr id='Vi1mY'><dt id='Vi1mY'><q id='Vi1mY'><span id='Vi1mY'><b id='Vi1mY'><form id='Vi1mY'><ins id='Vi1mY'></ins><ul id='Vi1mY'></ul><sub id='Vi1mY'></sub></form><legend id='Vi1mY'></legend><bdo id='Vi1mY'><pre id='Vi1mY'><center id='Vi1mY'></center></pre></bdo></b><th id='Vi1mY'></th></span></q></dt></tr></i><div id='Vi1mY'><tfoot id='Vi1mY'></tfoot><dl id='Vi1mY'><fieldset id='Vi1mY'></fieldset></dl></div>

                            <bdo id='Vi1mY'></bdo><ul id='Vi1mY'></ul>

                          • <small id='Vi1mY'></small><noframes id='Vi1mY'>