1

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)
0

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for sharing
I am tried but not work, maybe this happen due to the item value not the url but page title, tried with if 'Tutorial' in item.text:item.get_attribute('href').click(), Error occured object has no attribute 'click' , Please advice, thanks
You got that error because item.get_attribute('href') returns a string, not a Selenium object. If you have the item, just use item.click(). For more assistance, you'll need to include tracebacks and actual error text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.