简体   繁体   中英

Selenium python - spotify page

Im trying to click a follow button on a spotify page with selenium but it wont work. I've tried xpath and class_name but its not working.

The page im trying to click the follow button is page

Html Elemant:

<button type="button" class="aAr9nYtPsG7P2LRzciXc">Follow</button>

I think spotify randomizes the class names in order to prevent scrapping

My Code:

driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div[3]/main/div[2]/div[2]/div/div/div[2]/section/div/div[3]/div/button[1]').click()

Instead of using XPATH, you can use cssSelector which is much simpler and direct

driver.implicitly_wait(20)
button = driver.find_element_by_css_selector("button[class=aAr9nYtPsG7P2LRzciXc]")
button.click()

Edit: since everyone mentioned about waiting I added the implicit wait

Note: no, the driver wont wait 20 seconds but it will immediately execute after finding the element. 20 seconds is the maximum time you let it search for the button.

The Follow element is a dynamic element.

To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-testid='action-bar-row'] > button:not(aria-label)"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Follow']"))).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

Looks like you are simply missing a delay.
The best way to do that is to use Expected Conditions explicit waits.
Also your locator can be improved.
With the following inports

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

After instantiating the wait object with

wait = WebDriverWait(driver, 20)

Your click action could be performed with:

wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='Follow']"))).click()

your code is fine.

but from my experience of "not getting an element from a page" the problem was that i didnt wait enough for the page to load.

try this:

from time import sleep

sleep(0.5) # or 1 second if its slower to load
driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div[3]/main/div[2]/div[2]/div/div/div[2]/section/div/div[3]/div/button[1]').click()

you dont have to wait 20 seconds, lol.

you just need some seconds before that element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM