1

So, I need to get the text (number of recovered people from covid) from this webpage into the console, but I can't find the class for the numbers can someone help me to locate the class, so I can print the numbers into the console. I need to use PhantomJS cuz I don't want the log to open when I run the code.

from selenium import webdriver


driver = webdriver.PhantomJS()
driver.get('https://www.tvnet.lv/covid19Live')

text = driver.find_element_by_class_name("covid-summary__count covid-c-recovered")
print(text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

2

find_element_by_class_name() expects a single class as an argument but you are providing two class names (class is a "multi-valued attribute", multiple values are separated by a space).

Either check for a single class:

driver.find_element_by_class_name("covid-c-recovered")

Or, switch to a CSS selector:

driver.find_element_by_css_selector(".covid-summary__count.covid-c-recovered")

Digging Deeper

Let's look at the source code. When elements are searched by class name, Python selenium actually constructs a CSS selector under the hood:

elif by == By.CLASS_NAME:
    by = By.CSS_SELECTOR
    value = ".%s" % value

This means that when you've used covid-summary__count covid-c-recovered as a class name value, the actual CSS selector that was used to find an element happened to be:

.covid-summary__count covid-c-recovered

which understandably did not match any elements (covid-c-recovered would be considered as a tag name here).

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

If you want the number make sure you have dots between class names.

driver.get('https://www.tvnet.lv/covid19Live')
element = driver.find_element_by_class_name("covid-summary__count.covid-c-recovered")
print(element.text)

Outputs

19 072
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

From per the documentation of selenium.webdriver.common.by implementation:

class selenium.webdriver.common.by.By
    Set of supported locator strategies.

    CLASS_NAME = 'class name'
    

So using find_element_by_class_name() you won't be able to pass multiple class names as it accepts a single class.


Solution

To print the number of people HEALED you can use either of the following Locator Strategies:

  • LATVIJĀ:

    print(driver.find_element_by_xpath("//h1[contains(., 'COVID-19 LATVIJĀ')]//following::ul[1]//p[@class='covid-summary__count covid-c-recovered']").text)
    
  • PASAULĒ:

    print(driver.find_element_by_xpath("//h1[contains(., 'COVID-19 PASAULĒ')]//following::ul[1]//p[@class='covid-summary__count covid-c-recovered']").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • LATVIJĀ:

    driver.get("https://www.tvnet.lv/covid19Live")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[contains(., 'COVID-19 LATVIJĀ')]//following::ul[1]//p[@class='covid-summary__count covid-c-recovered']"))).text)
    
  • PASAULĒ:

    driver.get("https://www.tvnet.lv/covid19Live")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[contains(., 'COVID-19 PASAULĒ')]//following::ul[1]//p[@class='covid-summary__count covid-c-recovered']"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Console Output:

    19 072
    52 546 925
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352