提问人:Vanity 提问时间:1/13/2019 更新时间:12/27/2020 访问量:3759
Selenium + Headless Chrome,提交凭据后 YouTube 无法连接到帐户
Selenium + Headless Chrome, YouTube doesn't connect to account after credentials submitted
问:
出于教育目的,我决定在 YouTube 上构建一些自动化脚本(由于涉及一些频道切换,无法通过 API 实现)。
我正在使用 Selenium 和 Headless Chrome。
当我将所有内容设置为 时,我的脚本工作正常,但那是因为我能够导入 ,这使我能够避免在登录过程中遇到的登录循环。headless-false
user-data-dir
因此,当我不导入数据时,我开始监控 headless-false 会发生什么,并且似乎出于某种原因它不想登录。
问题不在于我无法在登录名和密码字段中输入我的凭据,而在于发送凭据后 YouTube 无法登录我。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
options = webdriver.ChromeOptions()
#options.add_argument("user-data-dir=Admin$ /Users/Admin/Library/Application\ Support/Google/Chrome/Profile\ 30")
options.add_argument("window-size=1440,900")
options.add_argument('headless-false')
# launching the driver
driver = webdriver.Chrome(chrome_options=options)
# entering credentials
driver.get('https://www.youtube.com/')
time.sleep(1)
driver.find_element_by_xpath('//div[2]/ytd-button-renderer/a/paper-button/yt-formatted-string').click()
time.sleep(1)
email = driver.find_element_by_css_selector('input[type=email]')
email.send_keys('[email protected]')
print("Email entered")
driver.find_element_by_id("identifierNext").click()
time.sleep(1)
password = driver.find_element_by_css_selector('input[type=password]')
password.send_keys('mypassword')
print("Password entered")
driver.find_element_by_id("passwordNext").send_keys(Keys.ENTER)
print("Logged in!")
出于某种原因,当它在登录后返回 YouTube 时,该频道仍未登录。我尝试了许多不同的方法,例如尝试通过 youtube 移动网站,先登录 gmail,然后访问 YouTube,但无事可做。
我也知道使用不是做这些事情的好方法,我可以使用一些来代替,但我想在调整整个事情之前先解决主要问题。sleep ()
implicit wait
答:
0赞
Sufian.K
5/4/2020
#1
我认为这行不通,因为 youtube 看到登录过程是从无头浏览器/selenium 网络驱动程序完成的,并拒绝了它。 www.google.com 也有同样的问题(见 https://gist.github.com/ikegami-yukino/51b247080976cb41fe93)
评论