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


i trying click on first result on google result. here code entering chennai craiglist read csv file. sure first link come in organic result chennai.craiglist.org. quiet not sure how this.

from selenium import webdriver     selenium.webdriver.common.by import     selenium.webdriver.support.ui import select     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     open(filename, 'rb') 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 should come after line?

update

right giving

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

i not sure if work or not.

the xpath have chosen 'ok' not best.

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

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -