如何避免单击位于python selenium中相同位置的外部web元素

2024-06-16 17:28:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在https://onlineradiofm.in/stations/vividh-bharati自动点击播放按钮 在python中使用selenium

这是我的密码

import time 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome() 
url = "https://onlineradiofm.in/stations/vividh-bharati"
driver.get(url)
time.sleep(10)
play_button= WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div/div[1]/div/div[1]/div/div[2]/div[1]/pjsdiv[1]/pjsdiv[6]/pjsdiv[2]")))
play_button.click()
driver.quit()

我得到的错误是

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <pjsdiv style="position: absolute; top: -10px; left: -10px; pointer-events: none; opacity: 1; transition: opacity 0.1s linear 0s, transform 0.1s linear 0s; width: 20px; height: 20px; transform: scale(3); visibility: visible;">...</pjsdiv> is not clickable at point (214, 425). Other element would receive the click: <pjsdiv style="position: absolute; top: -10px; left: -10px; width: 20px; height: 20px; border-radius: 0px; background: rgb(0, 0, 0); opacity: 0; transition: opacity 0.1s linear 0s, background 0.1s linear 0s; cursor: pointer; transform: scale(3);"></pjsdiv>
  (Session info: chrome=88.0.4324.150)

我的分析是,兄弟web元素覆盖了我选择的web元素。因此出现了这个错误

你能建议一个解决这个问题的方法吗

谢谢


Tags: infromhttpsimportdivdriverseleniumtransform
1条回答
网友
1楼 · 发布于 2024-06-16 17:28:53

那真是一个粗鲁的家伙!如果有任何有用的东西来识别元素就好了

尝试一下这个点击-这对我来说很有用。这是一个新的xpath和一个不同的等待条件

driver = webdriver.Chrome() 
url = "https://onlineradiofm.in/stations/vividh-bharati"
driver.get(url)

play_button= WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='oframeplayer']/pjsdiv[6]/pjsdiv/pjsdiv")))
play_button.click()
time.sleep(10) # This should play for this long - the browser may close after the script completes

请注意,标识符//*[@id='oframeplayer']/pjsdiv[6]/pjsdiv/pjsdiv仍然是一个糟糕的标识符,但是没有更好的方法可以使用

相关问题 更多 >