I'm learning about python selenium, I want to click a youtube link in the youtube comment, can someone help me?
Example: 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>
Code trials :
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()
The problem is with your xpath, also the logic which you implemented to perform click operation could be more refined like this:
Hope this helps.
You were pretty close. To click on the desired comment with text as https://www.youtube.com/watch?v=PbLtyVcMrk0 within the url you need to induce WebDriverWait for the element to be clickable and you can use the following solution using useragent through Selenium and Python:
Code Block:
Console Output: