2017-10-17 130 views
2

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

下面是代码:

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选项?

将鼠标悬停在视频上方。

Before hovering over the video

悬停在视频上,点击AirPlay的按钮后。

After hovering over the video and clicking on the AirPlay button

+0

我看不到Airplay的选项。我假设我错过了一些触发它显示的组件。你能编辑你的问题并发布相关的HTML吗? – JeffC

+0

@ JeffC。我希望我已经提供了所需的信息。也许我需要提一下,这个元素可以在Mac电脑上的Safari浏览器中看到。我没有在Windows安装的Safari浏览器上尝试过。 –

回答

1

我没有在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); 
    
+0

我尝试了两个步骤。第二步似乎正在工作。我可以看到弹出的可用播放设备弹出。将花费一些时间在这方面让用户的方式工作。任何更多的想法都会非常有用。此外,第二步的更正将是“driver.execute_script(....)”。 –

+0

对于第一步,您可能需要在悬停和点击之间等待。更新了答案并更正了您的更正......我头脑中的语言太多......好。;) – JeffC

+0

我尝试使用WebDriverWait的第一步,但有趣的是,我得到TimeoutException等待元素可点击。在检查页面时,我注意到在按钮可见之前,“title = AirPlay”出现在html上,但是当我将鼠标悬停在AirPlay按钮上时,标题= Airplay失踪。我也附加了快照。文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/s upport/wait.py”,第80行,直到引发TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions。 TimeoutException:消息。 –

相关问题