user11669928
user11669928

Reputation:

Extract first element in the class using selenium

This search gives the result and there are multiple pincodes present. I only need to capture the first element in the class.

code :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
import html5lib
import json
import time
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 selenium.webdriver.common.keys import Keys
url = "https://www.google.com/"
chromedriver = r"C:\Users\me\chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.implicitly_wait(30)
driver.get(url)
search = driver.find_element_by_name('q')
search.send_keys("newyork pincode")
search.send_keys(Keys.RETURN) 



time.sleep(5) 
driver.quit()

Upvotes: 2

Views: 15900

Answers (3)

frianH
frianH

Reputation: 7563

You can use css selector or xpath to achieve.

  • Css Selector *recommended

    div.IAznY div.title
    
  • Xpath

    //div[@class="IAznY"]//div[@class="title"]
    

Add WebDriverWait instead time.sleep(..) for effeciency.

First you need following import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

note: find_element_by_* always return the first element, even if there are many elements with the same locator.

So you can use the bellow code:

search = driver.find_element_by_name('q')
search.send_keys("newyork pincode")
search.send_keys(Keys.RETURN)

element = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'div.IAznY div.title')))
print(element.text)

But if you need handle multiple like you say and you want not the first element, then you can use .find_elements_by_*. The code below is an example to get the second element:

elements = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.IAznY div.title')))
print(elements[1].text)

This is index [1], and you can see the difference between the first code and the second code in the WebDriverWait implementation, namely visibility_of_element_located andvisibility_of_all_elements_located

Upvotes: 2

KunduK
KunduK

Reputation: 33384

Induce WebDriverWait and Following Css Selector to identify the element.

elements=WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.mlo-c div.title')))
print(elements[0].text)

Printed on console:

 10001

You need to import followings to execute above code.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 0

Spark
Spark

Reputation: 2487

Finding the class with the name h998We mlo-c and getting the first element will give you the first pin code.

find_element_by_class_name('h998We mlo-c') and it will return you all the pin codes for any city such as Delhi which you were asking earlier, and then get the text inside the div to get the Pincode

https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_class_name

Upvotes: -1

Related Questions