Hi
In below code i want to loop through videos indefinitely which are defined in variable videos.
C#
var videos = new List
{
"htps://google.com",
"htps://asp.net",
"htps://youtu.be/iuMqdF2Lb_4"
};
await Task.Run(async () =>
{
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
_driver = new ChromeDriver(service, options);
_driver
.Manage()
.Window
.Size = new Size(1024, 768);
_driver.Navigate().GoToUrl($"{BASE_URL}?v={"_KOo0"}");
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)
{
_driver.Navigate().Refresh();
}
}
});Thanks

Ramco RamcoRank #462 · 5.4K reputationPosted Aug 4, 2024, 8:25 AM EDT
Hi ABhishek
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
Abhishek YadavRank #217 · 11.3K reputationPosted Aug 4, 2024, 7:40 AM EDT
To loop through the list of videos indefinitely and handle the playback in your Selenium WebDriver setup, you can modify your code to iterate over the
videoslist and continuously play each video. Here’s how you can do it:videoslist.Navigate().GoToUrlcall to use the current video URL.Here’s the updated code:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading.Tasks; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; var videos = new List { "https://google.com", "https://asp.net", "https://youtu.be/iuMqdF2Lb_4" }; await Task.Run(async () => { var service = ChromeDriverService.CreateDefaultService(); service.HideCommandPromptWindow = true; var options = new ChromeOptions(); // Add options if needed 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 } } } } }); // Helper method to format duration string FormatDuration(string durationText) { // Implement formatting based on the actual text format return durationText; }foreach (var videoUrl in videos): This loop iterates over each video URL in thevideoslist.driver.Navigate().GoToUrl(videoUrl): Navigates to the current video URL.await Task.Delay(2000): Waits for 2 seconds to ensure the page loads before proceeding.Inner
while (true)Loop: Monitors the playback time and breaks out of the loop when the video finishes.await Task.Delay(1000): Adds a delay to avoid excessive checking in the playback monitoring loop.using (var driver = new ChromeDriver(service, options)): Ensures theChromeDriverinstance is properly disposed of after use.This setup will loop through each video indefinitely, handle playback, and refresh the video when it finishes. Adjust the delays and other configurations as needed for your specific use case.