Skip to content
Advertisement

pycharm python selenium scraper apparently not printing correct value

I new to python, selenium, pycharm and such.

I’m trying to print the value of a on a website ( at the moment of writing this the value is 6320 ).The code is not giving errors but it’s printing nothing.

As you can see in the screenshot, when i’m debugging and hovering over the variable, it’s displaying 6320, which is the value i’m looking for.

What am I doing wrong?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located

# This example requires Selenium WebDriver 3.13 or newer
with webdriver.Chrome() as driver:
    wait = WebDriverWait(driver, 10)
    driver.get("https://ici.radio-canada.ca/info/2020/coronavirus-covid-19-pandemie-cas-carte-maladie-symptomes-propagation/")

    driver.implicitly_wait(5)

    items = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[3]/div[3]/div[1]/table/tbody/tr[2]/td[1]/span[2]').text


    print(items)
    print("hello")

enter image description here

Thanks a lot for your help!

Advertisement

Answer

You need wait until .visibility_of_element_located and use this xpath: //td[contains(., "Total*")]//span[last()], although your xpath also works, but it is an absolute xpath that is vulnerable to change.

with webdriver.Chrome() as driver:
    wait = WebDriverWait(driver, 10)
    driver.get("https://ici.radio-canada.ca/info/2020/coronavirus-covid-19-pandemie-cas-carte-maladie-symptomes-propagation/")
    items = wait.until(EC.visibility_of_element_located((By.XPATH, '//td[contains(., "Total*")]//span[last()]'))).text
    print(items)
    print("hello")

Following import :

from selenium.webdriver.support import expected_conditions as EC
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement