Skip to content
Advertisement

Unable to locate element by class name using selenium via Python. Why so?

I wrote the following code in order to scrape the text of the element <h3 class="h4 mb-10">Total nodes: 1,587</h3> from https://blockchair.com/dogecoin/nodes.

JavaScript

I’m aware that there are perhaps less bloated options than selenium to scrape the target in question, yet the said code is just a snippet, a part of a more extensive script that relies on selenium for its other tasks. Thus let us limit the scope of the answers to selenium only.

Although there are three elements of the class "h4 mb-10" on the page, I am unable to locate the element. When I call driver.find_element_by_class_name("h4 mb-10"), I get:

JavaScript

Applying waits, currently commented out in the snippet, was to no avail. I came across this question and so I tried calling WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, ".h4 mb-10"))). I got :

JavaScript

I have no clue what am I doing wrong. Is it doable to scrape the target with selenium without using Xpaths?

Advertisement

Answer

Try with this xpath. Find all the elements with this xpath and with indexing extract the required Text.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement