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" >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()
CodePudding user response:
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()
CodePudding user response:
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.
CodePudding user response:
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.
CodePudding user response:
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
