Hi
In below code i want to loop through videos indefinitely which are defined in variable videos.
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 RamcoPosted Aug 4, 2024, 12:25 PM
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']"}
Thanks
Abhishek YadavPosted Aug 4, 2024, 11:40 AM
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:
Explanation: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.