What wrong am I doing. XPATH is correct but it says no element found. I also tried with find_elements(By.XPATH, "/html/body/div[3]/div[3]/div[5]/div[1]/table[]/tbody/tr[]/td[1]/a")
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By`enter code here`
driver = webdriver.Chrome(r'C:\\Users\\<Username>\\Downloads\\chromedriver_win32 (5)\\chromedriver.exe')
# rows = driver.find_elements_by_xpath("/html/body/div[3]/div[3]/div[5]/div[1]/table[*]/tbody/tr[*]/td[1]/a")
driver.get("https://www.census2011.co.in/data/subdistrict/5542-bangalore-north-bangalore-karnataka.html")
# time.sleep(15)
# rows = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div[2]/table/tbody/tr[*]/td[2]/a')
rows = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div[1]/div[2]/table/tbody/tr[*]/td[2]/a")))
print(rows)
data = [row.text.strip() for row in rows]
print(*data, sep = "\n")
# driver.close()
CodePudding user response:
Try this,
rows = driver.find_elements(By.XPATH, "//table[contains(@class, 'table table-striped table-hover')]/tbody/tr/td/a")
[row.get_attribute("href") for row in rows]
Output -
['https://www.census2011.co.in/data/town/803162-bbmp-karnataka.html',
'https://www.census2011.co.in/data/town/612950-chikkabanavara-karnataka.html',
'https://www.census2011.co.in/data/town/612949-hunasamaranahalli-karnataka.html',
'https://www.census2011.co.in/data/town/612951-madanaiyakanahalli-karnataka.html',
'https://www.census2011.co.in/data/town/612952-chikkabidarakallu-karnataka.html',
'https://www.census2011.co.in/data/town/612948-kadigenahalli-karnataka.html',
'https://www.census2011.co.in/data/village/612779-adakamaranahalli-karnataka.html',
....]