我一直在尝试通过网络抓取youtube视频,但每次启动google驱动程序时,都会出现一个弹出窗口。我试图通过让selenium为我施压来解决这个问题。我能用第一个。但第二个更棘手
是否有其他解决方法,或者是否存在错误,或者我可以用我的代码改进什么?为了澄清,我对第二张图片(我同意按钮)有问题。此代码的目标是按新注释对注释进行排序。有没有更好的方法用硒来做呢
代码:
import sys, unittest, time, datetime
import urllib.request, urllib.error, urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import InvalidArgumentException
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()
options.add_argument('--lang=en')
driver = webdriver.Chrome(executable_path=r'C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Email\chromeDriver\chromedriver.exe', chrome_options=options)
driver.get("https://www.youtube.com/watch?v=EV6PLN_8RBw")
time.sleep(5)
driver.find_elements_by_xpath("/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-upsell-dialog-renderer/div/div[3]/div[1]/yt-button-renderer")[0].click()
time.sleep(5)
#This Doesn't work
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'I agree')]")
for btn in buttons:
btn.click()
也许您可以用鼠标自动按下它:
我希望这是有帮助的
相关问题 更多 >
编程相关推荐