简体   繁体   中英

Selenium Python Google search, click chosen URL from result

I found following script, it is work good to print the result list by title. Please help to me to learn and add python script to click chosen url from google search result, for example click url from result which contain domain name "tutorial"

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 pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))
display.start()
driver = webdriver.Chrome()
driver.get("http://www.google.com")
input_element = driver.find_element_by_name("q")
input_element.send_keys("python")
input_element.submit()

RESULTS_LOCATOR = "//div/h3/a"

WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.XPATH, RESULTS_LOCATOR)))

page1_results = driver.find_elements(By.XPATH, RESULTS_LOCATOR)
for item in page1_results: print(item.text)

Try item.click(). The items are actually the Selenium link objects. So telling Selenium to click on them is all you need to go to the desired page. You can do selective clicking by checking the link text, and if you find what you're looking for, do item.click(). So let's say there was a link with the word 'Tutorial' in it on the page...

for item in page_results:
    if 'Tutorial' in item.text:
        item.click()
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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