我正在尝试使用 Selenium 将 youtube 视频转换到我的 chromecast。当我通常在 chrome 中打开 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 自动添加了一堆我不想要的标志,所以我不得不排除一个。
首先要找出我的配置文件存储在哪里,我打开了这个 url chrome://version/ 的 chrome。
这给了我很多信息,但重要的是
Command Line: /usr/lib/chromium-browser/chromium-browser --enable-pinch --flag-switches-begin --flag-switches-end
Profile Path: /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()
