2

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()
3
  • Finding the class with the name h998We mlo-c and getting the first element will give you the first pin code. Commented Sep 3, 2019 at 8:17
  • would the class name change for other pincodes Commented Sep 3, 2019 at 8:19
  • @thoris answer updated wit a bit explanation, hope helps more. Commented Sep 5, 2019 at 5:18

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

If you just want the first element, there's no reason to use .find_elements_*(), just use .find_element_*(). Also, the WebDriverWait() statement above returns the waited for element. By waiting and then doing another find elements, you are hitting the page twice. I've edited your answer to show you the streamlined way to do this.
I would argue (and it has been proven) that CSS selectors are faster than XPath. Given this locator doesn't require XPath, you can easily convert it to div.IAznY div.title, which is also much shorter and more readable (I think). I'll let you edit that in, if you want, because I feel it deviates too much from your intended answer for me to do.
Thanks very much, agree what you have done, very clear for me. You are one of the best stackoverflow users in the selenium class.
0

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

Comments

-1

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

3 Comments

Message: no such element: Unable to locate element: {"method":"css selector","selector":".h998We mlo-c"}
find_element_by_class_name() expects a single class name and you are sending it two. This will cause an error.
Got it let me rewrite my solution.

Your Answer

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