Home > database >  How to click on the play button with selenuim python
How to click on the play button with selenuim python

Time:04-24

I'm trying to click the play button on this website

Here is my code below

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time

browser = webdriver.Chrome()
browser.get("https://audiomack.com/iamrealtraffic")
time.sleep(10)
a = driver.find_element_by_xpath('//path[@d="M70.79 103c-.988 0-1.79-.802-1.79-1.79V69.866c0-.99.802-1.79 1.79-1.79l29.104 16.118s1.344 1.343 0 2.686C98.55 88.225 70.79 103 70.79 103"]')
webdriver.ActionChains(browser).move_to_element(a).click(a).perform()

CodePudding user response:

Are you sure you have the right xpath? I would try this xpath:

/html/body/div[2]/div[3]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div/div/span[1]/button

Also I would use implicitlywait instead of time.sleep.

CodePudding user response:

To click on the play icon you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://audiomack.com/black-sherif/song/kweku-the-traveler")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.row button[data-action='play']"))).click()
    
  • Using XPATH:

    driver.get("https://audiomack.com/black-sherif/song/kweku-the-traveler")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='row']//button[@data-action='play']"))).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
    
  • Related