我正在尝试点击这个网站上的播放按钮
下面是我的代码
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()
您确定使用了正确的xpath吗?我会尝试这样的xpath:
/html/身体/div [2]/div [3]/div/div [2]/div [1]/div/div/div/div/div/div [2]/div/div/跨度[1]/按钮
我还会使用隐含的wait而不是time.sleep.
点击播放图标,你需要为element_to_be_clickable()诱导WebDriverWait,你可以使用以下定位器策略:
使用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()
使用<<li>em> 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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC