Selenium webdriver click google search

后端 未结 7 923
情话喂你
情话喂你 2021-02-01 07:06

I\'m searching text \"Cheese!\" on google homepage and unsure how can I can click on the searched links after pressing the search button. For example I wanted to click the third

相关标签:
7条回答
  • 2021-02-01 08:06

    Most of the answers on this page are outdated.
    Here's an updated python version to search google and get all results href's:

    import urllib.parse
    import re
    from selenium import webdriver
    driver.get("https://google.com/")
    q = driver.find_element_by_name('q')
    q.send_keys("always look on the bright side of life monty python")
    q.submit();
    sleep(1)
    links= driver.find_elements_by_xpath("//h3[@class='r']//a")
    for link in links:
        url = urllib.parse.unquote(webElement.get_attribute("href")) # decode the url
        url = re.sub("^.*?(?:url\?q=)(.*?)&sa.*", r"\1", url, 0, re.IGNORECASE) # get the clean url
    

    Please note that the element id/name/class (@class='r') ** will change depending on the user agent**.
    The above code used PhantomJS default user agent.

    0 讨论(0)
提交回复
热议问题