1

I'm trying to crawling news comment. I want to crawling the text, '72' from this bluelined HTML code

enter image description here

So, this is my code

per_male = driver.find_element_by_css_selector('div.u_cbox_chart_progress u_cbox_chart_male > 
           span.u_cbox_chart_per')
print('per_male : ' + per_male.get_attribute('text'))

But I have this error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.u_cbox_chart_progress u_cbox_chart_male > span.u_cbox_chart_per"}
  (Session info: chrome=83.0.4103.97)

I also use this code

per_male = driver.find_element_by_css_selector('div.u_cbox_chart_progress u_cbox_chart_male > 
           span.u_cbox_chart_per')
print('per_male : ' + per_male.text)

But I have same error, how can I solve this problem?

Thx.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Hyungjoon Cho
  • 47
  • 1
  • 8

1 Answers1

1

To crawl the text 72 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.u_cbox_chart_progress.u_cbox_chart_male>div.u_cbox_chart_per"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='u_cbox_chart_progress u_cbox_chart_male']/div[@class='u_cbox_chart_per']"))).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
    

Reference

You can find a couple of relevant discussions on NoSuchElementException in:

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