【问题标题】:Python Webscraping Selenium Pop Up YoutubePython Web Scraping Selenium 弹出 Youtube
【发布时间】:2021-03-12 14:15:36
【问题描述】:

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

  • 我尝试使用 XPath 按下它,普通的和完整的详细的都有
  • 然后我尝试使用名称查找器按下它。但也没有成功。

是否有其他解决方法,或者有什么问题,或者我可以用我的代码改进什么?为了澄清,我对第二张图片有问题(我同意按钮)。此代码的目标是按 new 对 cme​​ts 进行排序。有没有更好的方法在 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()

【问题讨论】:

  • 好问题,我尝试使用 css 选择器,也没有成功,您可能需要使用显式等待,以确保它在视图中
  • 不幸的是那里也没有运气。命令:wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#introAgreeButton"))) 甚至没有找到对象。
  • 是 iframe 吗?

标签: python python-3.x selenium web-scraping selenium-chromedriver


【解决方案1】:

也许你可以用鼠标自动按下它:

pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.
pyautogui.click() 

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2017-05-14
    • 2020-01-16
    • 2020-09-19
    • 1970-01-01
    • 2019-01-06
    • 2018-11-11
    相关资源
    最近更新 更多