如何通过 Selenium 和 Python 使用用户代理在 youtube 评论中单击 youtube link

How to click youtube link within youtube comment using an user agent through Selenium and Python

我正在学习 python selenium,我想在 youtube 评论中点击一个 youtube link,有人可以帮助我吗?

示例:URL

Html :

<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/watch?v=PbLtyVcMrk0">https://www.youtube.com/watch?v=PbLtyVcMrk0&#65279;</a>

代码试验:

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)
driver.get("https://www.youtube.com/watch?v=NIWwJbo-9_8&lc=UgwNBxYVXb6uiVTioPB4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-uix-sessionlink  ' and contains(@href, '/watch?v=PbLtyVcMrk0')]"))).click()

你们非常接近。要单击文本为 https://www.youtube.com/watch?v=PbLtyVcMrk0 within the url 的所需评论,您需要引入 WebDriverWait 以使 元素可单击 您可以通过 Selenium 和 Python:

使用 useragent 使用以下解决方案
  • 代码块:

    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)
    driver.get("https://www.youtube.com/watch?v=NIWwJbo-9_8&lc=UgwNBxYVXb6uiVTioPB4AaABAg")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-uix-sessionlink       spf-link ' and contains(@href, '/watch?v=PbLtyVcMrk0')]"))).click()
    
  • 控制台输出:

    Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
    

问题出在你的 xpath 上,你实现的执行点击操作的逻辑也可以像这样更细化:

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)    
driver.get("https://www.youtube.com/watch?v=NIWwJbo-9_8&lc=UgwNBxYVXb6uiVTioPB4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "yt-formatted-string[class*='ytd-comment-renderer'][id='content-text']>a")))
clickLinks = driver.find_elements_by_css_selector("yt-formatted-string[class*='ytd-comment-renderer'][id='content-text']>a")
for element in clickLinks:
    if 'youtube' in element.text:
        element.click()

希望对您有所帮助。