Venkateshwaran Selvaraj
Venkateshwaran Selvaraj

Reputation: 1785

How to click on the first result on google using selenium python

I am trying to click on the first result on the google result. Here is my code where I am entering chennai craiglist which is read from csv file. So I am sure the first link that come in the organic result will be chennai.craiglist.org. But I am quiet not sure about how to do this.

from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    import unittest, time, re

    class Browse(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://google.com/"

    filename = 'test.csv'
    line_number = 1
    with open(filename, 'rb') as f:
        mycsv = csv.reader(f)
        mycsv = list(mycsv)
        self.cityname=mycsv[line_number][0]
        self.username=mycsv[line_number][1]
        self.password=mycsv[line_number][2]
        self.verificationErrors = []

def test_browse(self):
    driver = self.driver
    driver.get(self.base_url + "/")
    driver.find_element_by_id("gbqfq").send_keys(self.cityname)

I wanna know what should come after this line?

UPDATE

right now I am giving like

driver.find_elements_by_xpath(".//*[@id='rso']//div//h3/a")[:1].click()

I am not sure if it will work or not.

Upvotes: 5

Views: 15295

Answers (4)

user17115916
user17115916

Reputation: 11

  1. "iUh30" is class name of the first google search result irrespective of the keyword searched.
  2. .text will fetch url pointed by locator(class_name)
  3. driver.get() will navigate to the url

driver.get(driver.find_element_by_class_name("iUh30").text)

Upvotes: 1

Ben
Ben

Reputation: 178

I've been using driver.find_element_by_tag_name("cite").click() in python3 which has worked for me. However if you just want the link to the top search result it would be faster to use the requests and BeautifulSoup libraries as shown below

#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
url = 'http://www.google.com/search?q=something'
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
print(soup.find('cite').text)

Upvotes: 1

Jdushcuz
Jdushcuz

Reputation: 31

This works great with google results.

results = driver.find_elements_by_xpath('//div[@class="r"]/a/h3')  # finds webresults
results[0].click(). # clicks the first one

Upvotes: 3

Mark Rowlands
Mark Rowlands

Reputation: 5453

The xpath you have chosen is 'ok' but probably not the best.

result = driver.find_elements_by_xpath("//ol[@id="rso"]/li")[0] //make a list of results and get the first one
result.find_element_by_xpath("./div/h3/a").click() //click its href

Upvotes: 6

Related Questions