如何使用python硒在youtube注释中单击链接 [英] How to click a link within youtube comment using python selenium
问题描述
我是Selenium python的新手,我想单击youtube评论中的链接,但我不知道要使用什么元素?有人能帮我吗?谢谢 .例子:
I'm new to selenium python, I'm trying to click a link in the youtube comment but I don't know what element to use? Can someone help me? thanks . example :
<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/redirect?event=comments&redir_token=ttzJJRuKzz6sJPh8qdMnWStc-958MTU0NjE1NDU4M0AxNTQ2MDY4MTgz&stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg&q=https%3A%2F%2Fjulissars.itworks.com%2F" rel="nofollow">https://julissars.itworks.com</a>
推荐答案
要在
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 either of the following solution:
-
使用
PARTIAL_LINK_TEXT
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "julissars"))).click()
使用 CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.yt-simple-endpoint.style-scope.yt-formatted-string[href*='julissars']"))).click()
使用 XPATH
:
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()
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于如何使用python硒在youtube注释中单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!