Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98819

How can I find my site in the first 5 search results and follow the link

$
0
0

I tried the below code, but it's not correct exactly. How can I find my site in the first 5 search results and follow the link?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
# driver.maximize_window()

driver.get("http://google.com")
search = driver.find_element_by_name('q')
search.click()

search.send_keys('python')
search.send_keys(Keys.ENTER)

first_five = driver.find_elements_by_xpath(".//*[@id='rso']//div//h3/a")[:5]
link = first_five[:5].find_element_by_xpath('//*[@id="rso"]/div[4]/div/div[1]/div/div/div[1]/a/div/cite')
link.click()

#driver.quit()

Viewing all articles
Browse latest Browse all 98819

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>