How to click a link within youtube comment using python selenium

六眼飞鱼酱① 提交于 2021-02-04 08:26:31

问题


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&amp;redir_token=ttzJJRuKzz6sJPh8qdMnWStc-958MTU0NjE1NDU4M0AxNTQ2MDY4MTgz&amp;stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg&amp;q=https%3A%2F%2Fjulissars.itworks.com%2F" rel="nofollow">https://julissars.itworks.com&#65279;</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!