Skip to main content
 首页 » 编程设计

python之无法使用 selenium webdriver 在 Safari (mac) 上单击隐藏的 AirPlay 按钮元素

2025年01月19日45cyq1162

作为使用 Python 和 Selenium 进行测试的一部分,我打算使用 selenium 打开一个 youtube 链接,并希望单击“AirPlay”按钮以将其发送到 Apple TV。 最初我遇到了元素被隐藏的问题,但使用 ActionChains 解决了这个问题。脚本执行但我没有看到在播放的视频上执行点击,我可以看到 AppleTv 名称显示。

代码如下:

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By 
from selenium.webdriver import ActionChains 
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
import os , sys 
 
server_url = "http://10.0.10.4:4444/wd/hub" 
capabilities = DesiredCapabilities.SAFARI 
driver = webdriver.Remote(desired_capabilities=capabilities,  
command_executor=server_url) 
driver.get("https://www.youtube.com/watch?v=_YhrCp9m14k") 
 
#air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="move_player"]/div[28]/div[2]/div[2]/button[6]'))) 
air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'ytp-airplay-button'))) 
#air_play = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="text"]'))) 
 
#air_play = driver.find_element_by_css_selector('button.ytp-ariplay-button') 
hover = ActionChains(driver).move_to_element(air_play) 
hover.perform() 
 
hover1 = ActionChains(driver).click_and_hold(air_play).perform() 

html元素如下:

<button class="ytp-airplay-button ytp-button" title="AirPlay" style=""><svg  
height="100%" version="1.1" viewBox="0 0 36 36" width="100%"><use  
class="ytp-svg-shadow" NS1:href="#ytp-id-27"></use><path class="ytp-svg- 
fill" d="M12,28 L24,28 L18,22 L12,28 Z M27,9 L9,9 C7.9,9 7,9.9 7,11 L7,23  
C7,24.1 7.9,25 9,25 L13,25 L13,23 L9,23 L9,11 L27,11 L27,23 L23,23 L23,25  
L27,25 C28.1,25 29,24.1 29,23 L29,11 C29,9.9 28.1,9 27,9 L27,9 Z" id="ytp- 
id-27"></path></svg></ button> 

XPATH如下:

//*[@id="movie_player"]/div[28]/div[2]/div[2]/button[6] 

CSS选择器如下:

#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp- 
right- controls > button.ytp-airplay-button.ytp-button 

有人可以帮助解释为什么按钮没有被点击并显示可用的 Airplay 选项吗?

将鼠标悬停在视频上之前。

将鼠标悬停在视频上并点击 AirPlay 按钮后。

请您参考如下方法:

我在 Mac 上没有 Safari,所以我无法自己重现,但我猜测问题(根据您的屏幕截图)是在悬停视频区域之前图标不可见。按照设计,Selenium 不会与用户看不到的元素交互。有几种方法可以做到这一点。

  1. 用户方式...将鼠标悬停在视频上,悬停在图标上,然后单击图标。如果您正在尝试模拟用户,这就是您要使用的方法。

    from selenium import webdriver 
    from selenium.webdriver.common.action_chains import ActionChains 
    from selenium.webdriver.support import expected_conditions as EC 
     
    video = driver.find_element_by_css_selector("video") 
    ActionChains(driver).move_to_element(video).perform() 
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='AirPlay']")).click() 
    
  2. 非用户方式...使用 JSE 单击它。使用 JSE,我们可以单击任何元素,无论是否可见。没有用户可以单击不可见元素,因此如果您尝试模拟用户,请不要使用此...使用 #1。

    airplayButton = driver.find_element_by_css_selector("button[title='AirPlay']") 
    driver.execute_script("arguments[0].click();", airplayButton);