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

The placement of text appears to be incorrect when applying the float:left property in CSS

My issue is that I want #text1 to display on the right side of #video1, and #text2 to be positioned next to #video2. However, currently #text2 is also appearing beside #video1. Can someone please explain why this is happening and provide a solution to fi ...

Changing the text color of Material UI Accordion Summary upon expansion

How can I update the color of an Accordion summary text in Material UI when it is expanded? <Accordion expanded={expanded === 'panel1'} onChange={handleChange('panel1')} className={classes.root}> <AccordionSummary ...

Customize Text Area's Font Based on Selected Option in Dropdown List

I'm currently working on an HTML file that includes a dropdown list and a text area. Here's the code snippet: <select id='ddlViewBy' name='ddlViewBy' onchange='applyfonttotextarea()'> <option value = ' ...

Using relative positioning in CSS causes the div to move to a new line

Feel free to check out this demo I've created for reference: http://jsfiddle.net/jcb9xm44/ In a nutshell, the scenario involves two inline-block divs nested within a parent div: <div class="outer"> <div class="inner1"> Y & ...

Social media comments widget

Currently, I am incorporating the Facebook XML to implement a Facebook comments plugin within my MVC 3 application in the following manner: <fb:comments href="@Request.Url.AbsoluteUri" num_posts="15" width="900"></fb:comments> The issue lies ...

The Python script runs into a fatal error and displays an error message

A python script has been developed to interact with a web page and place an online order. The script includes a GUI feature that allows users to select the desired day for their order. Below is the Python code: import easygui import sys from selenium impo ...

Animate in Angular using transform without requiring absolute positioning after the animation is completed

Attempting to incorporate some fancy animations into my project, but running into layout issues when using position: absolute for the animation with transform. export function SlideLeft() { return trigger('slideLeft', [ state('void&a ...

The value of the variable is failing to be included in the Ajax request being sent to the server via jQuery

I'm encountering an issue with my contact form where I can't seem to retrieve the $_POST values via ajax for saving to a text file. It seems like there might be a problem with my javascript code. Check out the jQuery code snippet below: functio ...

Alternate. (individually)

Issue with Running Program I've been struggling to get my program to run correctly. The task at hand is to have the program display only one question at a time. But no matter what I try, clicking on all questions displays all the answers simultaneous ...

Displaying Data in a Table

In my current setup, I have a table that receives data from a form through JavaScript. The code is as follows: <div class="row"> <div class="span*"> <table id="production" class="table table-bordered" style="margin:10px auto;bor ...

Utilizing remote debugging for Internjs implemented on Selenium's ChromeDriver

When it comes to remotely debugging tests run by Internjs, the process involves running Chrome with Selenium and Chromedriver. To set up the debuggerAddress option for Chromedriver: debuggerAddress: '127.0.0.1:8765' However, upon running tests ...

Exploring the distinctions among different xpath implementations in Selenium WebDriver

Looking at this snippet of HTML code: <div id="divTooltips_Section_Filter" style="float:right; padding-right: 30px; padding-bottom: 10px;"> <img src="/mkteditor/css/images/tooltip.png" width="25px" height="25px" alt=""> </div> I've ...

How to create a fixed header navigation in SquareSpace

If you take a look at my Squarespace website using the Adirondack theme over here: The issue I'm facing is that whenever I scroll on either desktop or phone, the top header area shrinks. Currently, the navigation remains fixed on the screen and the l ...

Avoid positioning issues when dropping a DIV onto the Canvas

Utilizing the JavaScript code below allows me to drag a DIV onto a canvas. I am currently loading a multi-page PDF, which consists of image files, in a pop-up window and positioning canvas layers over them. In these canvas layers, shapes are then loaded at ...

Navigating Alerts with Headless Browsers (HtmlUnitDriver/Phantomjs Driver)

While utilizing the PhantomJs Driver for Headless Testing, I encountered the following exception: Sample code: import static org.testng.Assert.assertEquals; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.Capab ...

Using Selenium WebDriver to access values from the build.properties file and utilize them in another property file

Currently, I am working with Eclipse and Selenium WebDriver. In my project, I have set a URL to the variable 'webdriver.url' in the build.properties file. Additionally, I have created another property file named 'ExpectedResults.properties&a ...

Issues with mobile stylesheet loading properly

After migrating my website to a new server, I encountered an issue where the text displayed incorrectly on mobile devices but appeared fine on laptops. Surprisingly, when using Chrome's mobile viewer for inspection, everything looked as it should with ...

Guide on how to gather values into a variable from the DOM using jQuery

Trying to make this function: The current issue I'm facing is that when changing a digit (by clicking on a hexagon), I want to save the selected number as the value of a hidden input field next to the digit in the DOM. Any ideas on how to achieve th ...

Acquire the website link using Selenium in the Python programming language

As a beginner in Python, I am excited to dive into web scraping and have chosen the following website as my target: Link After some research, I believe that Selenium is the best tool for the job. To get started, I wrote the code snippet below: from selen ...

Tips for animating elements to move on scroll while navigating through the webpage

I came across a fascinating website that has a unique scrolling effect. As you scroll down the page, only the elements and images move while the page itself remains static, creating an interesting visual effect. I tried to replicate this on my own websit ...