jeudi 10 septembre 2020

Grabing text using Selenium/XPath/Python

I want to grep the total number of deaths from the Johns Hopkins Covid dashboard, see https://coronavirus.jhu.edu/map.html. I want to do this using Selenium, Python and Selenium’s chrome driver. The number of seaths can be found under the path //*[@id="ember1915"]/svg/g[2]/svg/text. enter image description here This is my script:

from selenium.webdriver import Chrome
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

with Chrome() as driver:
    driver.get('https://coronavirus.jhu.edu/map.html')
    driver.implicitly_wait(20) # Waits for 20 s for the entire page to loads.
    

    diplayElement = driver.find_element_by_xpath('//*[@id="ember1915"]/svg/g[2]/svg/text')

It fails with the error “no such element:

Unable to locate element: {"method":"xpath","selector":"//*[@id="ember1915"]/svg/g[2]/svg/text"}”.

This also happens for other sites I’m trying to scrape.

How can I fix this? What’s the reason for this error?

Thanks in advance,

Johannes




Aucun commentaire:

Enregistrer un commentaire