Hi
How to click Youtube like button using selenium . I am trying below code but not working
C#
var like_btn = _driver.FindElement(By.XPath("//div[@id='info']//ytd-toggle-button-renderer[1]//a[1]//yt-icon-button[1]//button[1]//yt-icon[1]"));Thanks
Hi
How to click Youtube like button using selenium . I am trying below code but not working
var like_btn = _driver.FindElement(By.XPath("//div[@id='info']//ytd-toggle-button-renderer[1]//a[1]//yt-icon-button[1]//button[1]//yt-icon[1]"));Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Apr 10, 2024, 11:04 PM EDT
here is a more general method using CSS selector
like_btn = _driver.find_element_by_css_selector("ytd-toggle-button-renderer.style-scope:nth-child(1) > a:nth-child(1) > yt-icon-button:nth-child(1)") like_btn.click()This CSS selector targets the like button based on its structure, and then the
.click()function simulates a click action.Remember to check the current structure of the YouTube page to ensure the selector is accurate. If YouTube's structure changes frequently, you may need to update your selector accordingly.
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 10, 2024, 11:25 PM EDT
Hi Prasad
I am getting error - No such element found
var like_btn = _driver.FindElement(By.CssSelector("ytd-toggle-button-renderer.style-scope:nth-child(1) > a:nth-child(1) > yt-icon-button:nth-child(1)"));Below is Script
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 11, 2024, 12:16 AM EDT
The issue might be with the XPath you’re using to locate the like button. YouTube’s page structure can change frequently, and the XPath you’re using might no longer be valid.
Here’s a general approach to click the like button on a YouTube video using Selenium:
// Navigate to the YouTube video _driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=videoId"); // replace 'videoId' with your video's ID // Wait for the page to load completely WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)); wait.Until(driver => driver.FindElement(By.CssSelector("body")).Displayed); // Click on the like button var likeButton = _driver.FindElement(By.CssSelector("button[aria-label='Like this video along with 1,000 other people']")); likeButton.Click();Please replace the
aria-labelvalue with the actual aria-label of the like button on your page. You can find this by inspecting the element in your browser.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 11, 2024, 1:05 AM EDT
Hi Naimish
Below line can change every time.
Like this video along with 1,000 other people'
Thanks