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.

1
  • Please edit your question with the actual html, not an image. Commented Jun 6, 2020 at 13:53

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

@HyungjoonCho Glad to be able to help you. Upvote the answer if this answer was helpful to you for the benefit of the future readers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.