Ramco Ramco

Ramco Ramco

  • 424
  • 3.4k
  • 484.7k

Error - no such element

Aug 5 2024 3:00 AM

Hi

When the second video starts running it gives this error 

no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(@class, 'ytp-play-button ytp-button') and @data-title-no-tooltip='Play']"}

await Task.Run(async () =>
{
    var service = ChromeDriverService.CreateDefaultService();
    service.HideCommandPromptWindow = true;

    var options = new ChromeOptions();


    using (var driver = new ChromeDriver(service, options))
    {
        driver.Manage().Window.Size = new Size(1024, 768);
        while (true)
        {
            foreach (var videoUrl in videos)
            {
                driver.Navigate().GoToUrl(videoUrl);
                await Task.Delay(2000); // Allow time for the page to load

                var durationElement = driver.FindElement(By.XPath(XPATH_TIME));
                var totalDuration = TimeSpan.Parse("00:" + durationElement.Text);
                var btnPlay = driver.FindElement(By.XPath(XPATH_PLAY));
                btnPlay?.Click();

                while (true)
                {
                    var currentTimeElement = driver.FindElement(By.ClassName("ytp-time-current"));
                    var currentTime = TimeSpan.Parse(FormatDuration(currentTimeElement.Text));

                    if (currentTime >= totalDuration)
                    {
                        break; // Exit the inner loop to go to the next video
                    }
                    await Task.Delay(1000); // Check every second
                }
            }
        }
    }
});

Thanks


Answers (3)