Python网站弹出Youtube

2024-04-25 12:12:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试通过网络抓取youtube视频,但每次启动google驱动程序时,都会出现一个弹出窗口。我试图通过让selenium为我施压来解决这个问题。我能用第一个。但第二个更棘手

  • 我试着使用XPath按下它,包括普通的和详细的
  • 然后我试着用名字查找器按下它。但也没有成功

是否有其他解决方法,或者是否存在错误,或者我可以用我的代码改进什么?为了澄清,我对第二张图片(我同意按钮)有问题。此代码的目标是按新注释对注释进行排序。有没有更好的方法用硒来做呢

代码:

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()

Was able to press this oneBut not this one


Tags: 方法代码fromimportdivsupportbytime