1

i'm trying to locate the subscribe button in Youtube then click it, but i'm not able to do that.

My actual code :

public void start()
        {
            // Initialize the instance
            driver = new ChromeDriver();
            // Navigate to the url
            driver.Navigate().GoToUrl("https://www.youtube.com/channel/UCaJBePyIsBIp4MtVFpPbtJg");
            // 1.5
            Thread.Sleep(1500);
            // Find and Click on button
            driver.FindElement(By.CssSelector(".style-scope.ytd-button-renderer.style-destructive.size-default")).Click();
        }

My two methods tried :

// METHOD 1
driver.FindElement(By.CssSelector("style-scope ytd-button-renderer style-destructive size-default")).Click();
renderer').click()");


// METHOD 2
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("document.getElementById('#subscribe-button > ytd-button-renderer').click()");
2
  • Please edit your question according to this guide: stackoverflow.com/help/how-to-ask Commented Apr 28, 2019 at 17:40
  • A better question is why you're trying to do that; there are few, if any, legitimate use-case scenarios for this. 🤨 It sounds like click-fraud. Commented Apr 3, 2021 at 17:37

2 Answers 2

2

It looks like the problem you are running into with your CSS selector is that you are trying to use a class with spaces in it, which is interpreted as a set of distinct classes. The css selector should work (assuming the class you've listed is correct) if you try

driver.FindElement(By.CssSelector(".style-scope.ytd-button-renderer.style-destructive.size-default")).Click();

I would not use JavaScript executor for a simple click such as this.

Sign up to request clarification or add additional context in comments.

4 Comments

...I would not use JavaScript executor for a simple click such as this... Niche idea
In visual studio get this error message: no such element: Unable to locate element: {"method":"css selector","selector":".style-scope.ytd-button-renderer.style-destructive.size-default"} I inspect element and button it is the html of button : <yt-formatted-string id="text" class="style-scope ytd-button-renderer style-destructive size-default">Suscribirse <span class="deemphasize style-scope yt-formatted-string">749</span></yt-formatted-string>
Can you please edit your question to include the full code from your script? If that is the correct class it should definitely work provided the page has loaded with that element.
@Gexur is the page loading visibly when you run the script? I believe you need to specify an IWebDriver object type when initializing your browser, eg IWebDriver driver = new ChromeDriver();
0

You have wrong css selector.And try to use xpath selector.

driver.FindElement(By.XPath("//*[@id=\"subscribe-button\"]/ytd-button-renderer/a")).Click();

This should work

10 Comments

Hmm. This doesn't find any elements when I try it on the asker's web page.
Hi, thanks for the help, it does not work for me with that code either.
really?I have tested in on youtube
Have you tried it on the user's webpage, with URL of https://www.youtube.com/channel/UCaJBePyIsBIp4MtVFpPbtJg?
Try again please.You had to say that you are trying to do that on channel page,not on video page
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.