如何通过 Selenium 和 Python 使用用户代理单击 youtube 评论中的链接 [英] How to click a link within youtube comment using an user agent through Selenium and Python

查看:23
本文介绍了如何通过 Selenium 和 Python 使用用户代理单击 youtube 评论中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个点击 youtube 评论中的链接的脚本,它工作正常,但是当我将它与用户代理结合时它不起作用,有人可以帮助我吗?

I am writing a script that clicks a link in the youtube comment, it works fine, but when I combine it with the user agent it doesn't work, can someone help me?

示例:链接

HTML:

<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/redirect?redir_token=ix1uBK3TO3bxXdcT7EvDFp-vI9p8MTU0NjQxNTExOEAxNTQ2MzI4NzE4&amp;event=comments&amp;q=https%3A%2F%2Fjulissars.itworks.com%2F&amp;stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg" rel="nofollow">https://julissars.itworks.com&#65279;</a>

代码试用(没有用户代理,它可以工作):

Code trial ( without user agent , it works ) :

from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and contains(., 'julissars')]"))).click()

代码试用(使用用户代理,它不起作用):

Code trial ( with user agent , it doesn't work ) :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent

useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", useragent.random)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and contains(., 'julissars')]"))).click()

推荐答案

点击所需的评论,文本为 https://julissars.itworks.com 位于 url您需要引入 WebDriverwait 以使元素可点击,您可以使用 useragent 通过 SeleniumPython:

To click on the desired comment with text as https://julissars.itworks.com within the url you need to induce WebDriverwait for the element to be clickable and you can use the following solution using useragent through Selenium and Python:

  • 代码块:

  • Code Block:

from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

ua = UserAgent()
options = webdriver.ChromeOptions()
userAgent = ua.random
print(userAgent)
options.add_argument('user-agent=' + userAgent)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-uix-sessionlink  ' and contains(@href, 'julissars')]"))).click()

  • 控制台输出:

  • Console Output:

    Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
    

  • 您可以在如何在 Selenium 中更改 Google Chrome 用户代理?

    这篇关于如何通过 Selenium 和 Python 使用用户代理单击 youtube 评论中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆