How to get same class name seperately by using css_selector?

Issue

I am using the below code to get data from http://www.bddk.org.tr/BultenHaftalik. Two table elements have the same class name. How can I get just one of them?

from selenium import webdriver
import time

driver_path = "C:\\Users\\Bacanli\\Desktop\\chromedriver.exe"
browser = webdriver.Chrome(driver_path)
browser.get("http://www.bddk.org.tr/BultenHaftalik")
time.sleep(3)

Krediler = browser.find_element_by_xpath("//*[@id='tabloListesiItem-253']/span")
Krediler.click()

elements = browser.find_elements_by_css_selector("td.ortala")

for element in elements:
    print(element.text)

browser.close()

Solution

If you want to select all rows for one column only that match a specific css selection, then you can use :nth-child() selector.

Simply, the code will be like this:

elements = browser.find_elements_by_css_selector("td.ortala:nth-child(2)")

In this way, you will get the "Krediler" column rows only. You can also select the first child if you want to by applying the same idea.

Answered By – Hussein Basil

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published