How to scrape data from webpage which uses react.js with Selenium in Python?

前端 未结 1 892
野的像风
野的像风 2021-01-22 05:15

I am facing some difficulties scraping a website which uses react.js and not sure why this is happening.

This is the html of the website:

What I wi

1条回答
  •  粉色の甜心
    2021-01-22 05:38

    Seems you were close. While using find_element_by_class_name() you can't pass multiple classes and you are allowed to pass only one classname, i.e. only only one among either of the following:

    • play-pause-button
    • btn
    • btn-naked

    On passing multiple classes through find_element_by_class_name() you will face Message: invalid selector: Compound class names not permitted


    Solution

    As an alternative, as the element is an Angular element, to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.play-pause-button.btn.btn-naked")))click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='play-pause-button btn btn-naked']")))click()
      
    • 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
      

    0 讨论(0)
提交回复
热议问题