问题
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>
link
回答1:
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:
Using
PARTIAL_LINK_TEXT
:WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "julissars"))).click()
Using
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()
Using
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
来源:https://stackoverflow.com/questions/53968183/how-to-click-a-link-within-youtube-comment-using-python-selenium