2017-10-17 79 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:消息。 –

相關問題