2017-06-20 18 views
0

我想從下面的YouTube視頻獲得成績單(我知道有一種方法可以獲得cc字幕(如果可用,但通常不是)。點擊動態加載的菜單與python phantomjs

我使用phantomjs在python中做到這一點。

url = 'https://www.youtube.com/watch?v=wOn8xawC-HQ' 

phantom_driver = webdriver.PhantomJS(executable_path="./phantomjs-2.1.1- macosx/bin/phantomjs") 

phantom_driver.get(url) 

的成績單隻有在點擊後出現「更多...」按鈕,我可以訪問:

phantom_driver.find_element_by_id('action-panel-overflow-button').click() 

...這創建了含有該化合物類"yt-uix-menu-content yt-ui-menu-content yt-uix-kbd-nav yt-uix-menu-content-hidden"一個div菜單中有一個由4個元素組成的<ul>

我需要點擊這四個元素中的一個打開轉錄框(具體來說,我需要點擊複合類"yt-ui-menu-item has-icon yt-uix-menu-close-on-select action-panel-trigger action-panel-trigger-transcript")。

但是,運行

phantom_driver.find_element_by_class_name('action-panel-trigger-transcript').click() 

不起作用,因爲該元素似乎還隱藏(Element is not currently visible and may not be manipulated)。

我相信我需要的是點擊「更多...」按鈕,然後直接點擊打開的菜單。

我已查看this post關於選擇menues和this one關於使用html後點擊。我也沒有解決我的問題。

我在腳本中丟失了什麼?點擊後需要「刷新」檢索到的HTML嗎?

回答

0

「動作面板溢出按鈕」是按鈕ID不是名稱。 通過它的XPath

您可以點擊此元素能正常工作:

phantom_driver.find_element_by_xpath('//*[@id="action-panel-overflow-button"]').click() 
phantom_driver.find_element_by_xpath('//*[@class="yt-ui-menu-item has-icon yt-uix-menu-close-on-select action-panel-trigger action-panel-trigger-transcript"]').click()