【问题标题】:How to click youtube link within youtube comment using an user agent through Selenium and Python如何通过 Selenium 和 Python 使用用户代理在 youtube 评论中单击 youtube 链接
【发布时间】:2019-01-04 10:16:27
【问题描述】:

我正在学习 python selenium,我想点击 youtube 评论中的 youtube 链接,有人可以帮我吗?

示例: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</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()

【问题讨论】:

    标签: python selenium youtube user-agent webdriverwait


    【解决方案1】:

    你已经很接近了。要在url 中单击带有文本为 https://www.youtube.com/watch?v=PbLtyVcMrk0 的所需评论,您需要诱导 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
      

    【讨论】:

      【解决方案2】:

      问题出在你的 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()
      

      希望这会有所帮助。

      【讨论】:

      • 在 id 中,我将其设置为 '' / 观看? v = PbLtyVcMrk0 '' 但它不起作用!非常感谢learner8269,我找到了解决办法。
      猜你喜欢
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2014-04-24
      • 2014-10-05
      • 2020-09-10
      • 2018-02-08
      相关资源
      最近更新 更多