Unable to find the XPATH element with Selenium in Python due to element not being located

Can anyone help me troubleshoot? My XPATH seems correct, but it's showing 'no element found' error. I also attempted using 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")

# 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()

Answer №1

Give this a shot:

elements = driver.find_elements(By.XPATH, "//table[contains(@class, 'table table-striped table-hover')]/tbody/tr/td/a")
[elem.get_attribute("href") for elem in elements]

Here is the resulting 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',
....]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with the startWith() method in Selenium Webdriver with Java?

I'm encountering an issue where my startsWith() method in Selenium using Java is appearing red. I am new to both Selenium and Java, so any guidance would be greatly appreciated. I am currently working with IntelliJ IDEA community version on a Windows ...

Utilizing Selenium to extract data from a table and displaying the results

I am looking to scrape tables from a specific website, but I am new to automation with Selenium. Here is the code snippet that I have come up with after doing some research: from selenium.webdriver import Firefox from selenium.webdriver.common.by import By ...

Menu within a menu, accessed by hovering

<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <a href="index.html" class="navbar-brand display-4">NintendoWorlds</a> <button class="navbar-toggler" dat ...

Stop the anchor tag from activating Chrome's status bar

I've been working on a web application with an elastic layout where the footer acts as a toolbar and information display. The footer is always visible at the bottom of the page, without any need for scrolling. We specifically chose this location for t ...

As the div elements stack up and you scroll towards the top or bottom

I am dealing with two nested DIVs that are both scrollable. When I scroll the "inside div" (the green one in the attached file), and reach the bottom of the DIV, it starts scrolling the DIV beneath it. How can I prevent the DIV beneath from scrolling only ...

Troubleshooting: jQuery click event not functioning correctly with if-else statement

For some reason, I can't seem to figure out how to toggle the color of all list items with a class of "link" between blue and black. I've searched through numerous posts but still can't find a solution. Working Example: http://jsfiddle.net/ ...

Issue arising from selenium.common.exceptions.ElementClickInterceptedException: Description: element click intercepted:

My current project involves scraping company names and leads from LinkedIn Sales Navigator. Although I am successful in retrieving the company names, I am facing difficulties in obtaining the company leads - the names of people associated with the navigato ...

Is there a different way to manage multiple if and if-else conditions based on the presence of elements in Selenium testing?

While working on automating e2e automation cases, I encountered a problem with performance due to using multiple if conditions within a function for increased reusability. Does anyone have suggestions for alternative approaches to improve performance witho ...

Internet Explorer 9 is not fully extending the width of the box for multiple select options

I am facing an issue with a multiple select input in Internet Explorer 9. The options are not utilizing the full width of the select box, despite having a min-width set on the select element. In Chrome and Firefox, the items fill up the available width pe ...

Tips for avoiding the security risk of "A potentially hazardous Request.Form" in requests

After integrating a FreeTextBox control into my webpage, users can input HTML tags. However, upon submitting to the server, I encounter the following error: A potentially dangerous Request.Form value was detected from the client (GestisciPagine1_txtTest ...

NextJs only displays loading animation once

Currently, I am developing an app using Next.js and I am facing a challenge with implementing a loading animation. The animation I am attempting to incorporate consists of three bouncing balls which display correctly. When I launch the Next.js app with n ...

Overlay a small image on top of a larger background image using canvas, then merge them into a single set of pixel

Is there a way to combine a smaller image with a larger background image on one canvas while allowing the smaller image to move around? I'll need to save the original background pixel data so that each frame can be redrawn with the overlay in its upda ...

The footer seems to detach on certain pages

Currently, I am facing a frustrating issue while working on a website. The footer seems to be disconnected from the main body content on certain pages, while on others, it appears to be properly attached. Despite my efforts to troubleshoot by removing elem ...

When deployed, Angular 14 and Bootsrap 5 are functioning properly on a local environment but encountering issues on an online

My Angular application (version 14.2.0) is integrated with bootstrap (version 5.2.3). Upon building and deploying the application on a local IIS server, everything displays correctly as shown in the image below: https://i.sstatic.net/pLDs6.jpg However, w ...

Having trouble changing a CSS property (margin-right)?

I have created a simple HTML list that consists of various items: <div id="menu"> <ul> <li>apples</li> <li>&#149</li> <li>bananas</li> <li>oranges</li> <li>grape ...

Sliding out the sidebar menu with Jquery

Hey there! I'm currently working on implementing a swipe feature for the side menu bar. I have already added a click method in my code, but now I also need to incorporate a swipe technique. When the side bar is opened, I want to remove the inside im ...

Scrollbar malfunction in Angular and Bootstrap主 The main scrollbar in Angular and Bootstrap is un

I am currently facing an issue with my Angular app using Bootstrap 5. The main html/body scrollbar does not work when elements overflow the view. I have tried various solutions such as setting a fixed height, adjusting overflow-y to scroll or auto for body ...

Page jumping vertically in Chrome upon reload, with Firefox and Internet Explorer functioning properly

Utilizing this jQuery script, I am able to center a website vertically within the browser window if it exceeds the height of the outer wrapper-div, which has fixed dimensions. $( document ).ready(function() { centerPage(); )}; // center page vertic ...

The code provided creates a web form using HTML and ExpressJS to store submitted information into a MongoDB database. However, there seems to be an issue with the 'post' method, while the 'get' method functions correctly

When I try to submit entries (name, email, address) on localhost:3000 in the browser, the 'post' function is not creating an object in the mongo database even though it works from the postman. The browser displays an error message saying "unable ...

Exploring the Features of PrimeNG Table Component in Angular 8

After attempting to implement p-table (PrimeNG table) in my Angular project and importing all necessary dependencies and modules using the CLI, I encountered the following error: ERROR: The target entry-point "primeng/table" has missing dependencies: - @ ...