我正在尝试使用Selenium将YouTube视频投射到我的chromecast。当我用铬打开youtube时,我看到了转换按钮,它工作正常。当我用Selenium打开它时,丢失了投射按钮,当我从菜单中选择“投射”时,它给出了错误“找不到投射目的地。需要帮助吗?”
我正在使用python,并尝试了许多与webdriver的标志组合。这就是我所拥有的
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=./ChromeProfile')
options.add_argument('--disable-session-crashed-bubble')
options.add_argument('--disable-save-password-bubble')
options.add_argument('--disable-permissions-bubbles')
options.add_argument('--bwsi')
options.add_argument('--load-media-router-component-extension')
options.add_argument('--enable-video-player-chromecast-support');
excludeList = ['disable-component-update',
'ignore-certificate-errors',
]
options.add_experimental_option('excludeSwitches', excludeList)
chromedriverPath = '/my/path/to/chromedriver'
driver = webdriver.Chrome(chromedriverPath, chrome_options=options)
path = 'https://www.youtube.com/watch?v=Bz9Lza059NU'
driver.get(path);
time.sleep(60) # Let the user actually see something!
driver.quit()
我想出了如何使它工作。这似乎需要两个步骤。将我的默认配置文件复制到某个地方selenium可以使用它,并找出打开chrome时使用的正确标志。密钥是selenium自动添加了一堆我不想要的标志,所以我不得不排除一个。
首先找出我的个人资料存储位置,我打开了这个网址chrome://version/
的chrome。
这给了我很多信息,但重要的是
命令行:/ usr / lib / chromium-browser / chromium-browser --enable-pinch --flag-switches-begin --flag-switches-end
配置文件路径:/home/mdorrell/.config/chromium/Default
首先,我将我的个人资料复制到Selenium可以使用的某个目录中
cp -R /home/mdorrell/.config/chromium/Default/* /home/mdorrell/ChromeProfile
然后我在selenium打开的浏览器中打开了同一页面,并获得了从Command Line
行添加selenium的标志列表。最终给我问题的是--disable-default-apps
最后,我需要添加的代码最终看起来像这样
options = webdriver.ChromeOptions()
# Set the user data directory
options.add_argument('--user-data-dir=/home/mdorrell/ChromeProfile')
# get list of flags selenium adds that we want to exclude
excludeList = [
'disable-default-apps',
]
options.add_experimental_option('excludeSwitches', excludeList)
chromedriverPath = '/my/path/to/chromedriver'
driver = webdriver.Chrome(chromedriverPath, chrome_options=options)
path = 'https://www.youtube.com/watch?v=Bz9Lza059NU'
driver.get(path);
time.sleep(60) # Let the user actually see something!
driver.quit()
感谢@MikeD分享您的回答。
当我想通过selenium浏览器(使用RSelenium)镀铬R Shiny Dashboard时,我遇到了同样的问题。如果我点击Cast
,它会显示“找不到播放目的地。需要帮助吗?”,而从普通浏览器中它可以正常工作。
在我的情况下,它在排除两个开关(包括不需要ChromeProfile)后工作,在R中可以完成:
library(RSelenium)
options <- list()
options$chromeOptions$excludeSwitches <- list('disable-background-networking',
'disable-default-apps')
rD <- rsDriver(verbose = FALSE, port = 4570L, extraCapabilities = options)