1

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()
jack12
  • 11
  • 3
  • What element are you trying to access on what exactly page? – Prophet Jan 24 '22 at 14:47
  • @Prophet its the "follow" button on this page => https://open.spotify.com/user/94ueka5j8y1jt466781y2frlp?si=rHBZSsT_TS2VHujyPKvMUw&utm_source=copy-link&nd=1 – PSR Jan 24 '22 at 14:48
  • @PSR how did you guess that? – Prophet Jan 24 '22 at 14:54
  • 1
    "", from this i understand its a button called "Follow", and OP has linked the site as well. @Prophet – PSR Jan 24 '22 at 14:57

4 Answers4

0

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.

CherrieP
  • 21
  • 3
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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()
Prophet
  • 32,350
  • 22
  • 54
  • 79
0

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.

alexzander
  • 1,586
  • 1
  • 9
  • 21