Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用selenium webdriver在Safari(mac)上单击隐藏的AirPlay按钮元素_Python_Selenium_Selenium Webdriver_Safari - Fatal编程技术网

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

Python 无法使用selenium webdriver在Safari(mac)上单击隐藏的AirPlay按钮元素,python,selenium,selenium-webdriver,safari,Python,Selenium,Selenium Webdriver,Safari,作为使用Python和Selenium进行测试的一部分,我打算使用Selenium打开一个youtube链接,并希望单击“AirPlay”按钮将其发送到Apple TV。 起初,我有隐藏元素的问题,但是使用ActionChains解决了这个问题。脚本执行了,但我没有看到在播放的视频上执行单击,我可以看到AppleTv名称显示出来 代码如下: from selenium import webdriver from selenium.common.exceptions import NoSuchEl

作为使用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>
//*[@id="movie_player"]/div[28]/div[2]/div[2]/button[6]
#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-
right- controls > button.ytp-airplay-button.ytp-button
CSS选择器如下所示:

<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>
//*[@id="movie_player"]/div[28]/div[2]/div[2]/button[6]
#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不会与用户看不到的元素交互。有几种方法可以做到这一点

  • 用户方式。。。将视频悬停,将图标悬停,然后单击图标。如果您试图模拟用户,这就是您想要使用的方法

    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()
    
  • 非用户方式。。。用JSE点击它。使用JSE,我们可以单击任何元素,无论是否可见。没有用户可以单击不可见元素,因此如果您试图模拟用户,请不要使用此。。。使用#1


  • 我看不到Airplay选项。我假设我丢失了一些触发它出现的组件。你能编辑你的问题并发布相关的HTML吗?@JeffC。我希望我已经提供了您所需要的信息。也许我需要提一下,这个元素在mac books上的Safari浏览器上可以看到。我没有在Windows安装的Safari浏览器上尝试过。我尝试了这两个步骤。第二步似乎奏效了。我可以看到弹出窗口显示可用的airplay设备。将在这方面花费一些时间,以使用户方式起作用。任何更多的想法都会非常有用。另外,第二步的修正是“driver.execute_script(..)”。对于第一步,您可能需要在悬停和单击之间等待。更新答案并更正您的更正。。。我脑子里有太多的语言。。。接得好我使用WebDriverWait尝试了第一步,但有趣的是,我得到了TimeoutException,等待元素被单击。在查看页面时,我注意到在按钮可见之前,html上会显示“title=AirPlay”,但当我将鼠标悬停在AirPlay按钮上时,title=AirPlay就会消失。我也附上了快照。文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/s‌​upport/wait.py”,第80行,在until raise TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:message.尝试使用XPATH,“//button[@class='ytp-airplay-button ytp button']”即使这样也没有帮助。您可以使用路径上的id,ytp-id-32,看看是否有效。