Hi
I have saved Video Duration in a variable. I want once it reaches the end it should restart again & again
Below is the code
C#
await Task.Run(async () =>
{
_driver
.Navigate()
.GoToUrl($"{BASE_URL}?v={"uhaJ08EUb9A"}");
var timeElement = _driver.FindElement(By.XPath(XPATH_TIME));
timeValue = timeElement.GetAttribute("innerText");
var videoControls = new WebDriverWait(_driver, TimeSpan.FromSeconds(10))
.Until(driver => driver.FindElement(By.ClassName("ytp-chrome-controls")));
var action = new Actions(_driver);
action
.MoveToElement(videoControls)
.Perform();
});Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 10, 2024, 4:26 AM EDT
The values you provided for
currentTimeandtotalDurationare in the correct format forTimeSpan.Parse. The error might be occurring at a different time when these values are not in the correct format.One possibility could be when the video is buffering or loading, the
currentTimeElement.TextordurationElement.Textmight be empty or in an unexpected format.To handle this, you can add a check before parsing the time:
string FormatDuration(string duration) { if (string.IsNullOrEmpty(duration)) { return "00:00:00"; } var parts = duration.Split(':'); if (parts.Length == 2) // mm:ss format { duration = "00:" + duration; // convert to HH:mm:ss format } return duration; } // ... // Get the total duration of the video var durationElement = _driver.FindElement(By.ClassName("ytp-time-duration")); var totalDuration = TimeSpan.Parse(FormatDuration(durationElement.Text)); while (true) { // Get the current time of the video var currentTimeElement = _driver.FindElement(By.ClassName("ytp-time-current")); var currentTime = TimeSpan.Parse(FormatDuration(currentTimeElement.Text)); // ... }This
FormatDurationfunction now also checks if the duration is null or empty and if so, it returns “00:00:00”.Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 10, 2024, 1:01 AM EDT
You can achieve this by continuously checking the current video time and comparing it with the total video duration. Once the current time reaches the end of the video, you can refresh the page to restart the video. Here’s how you can modify your code:
await Task.Run(async () => { _driver.Navigate().GoToUrl($"{BASE_URL}?v={"uhaJ08EUb9A"}"); var timeElement = _driver.FindElement(By.XPath(XPATH_TIME)); timeValue = timeElement.GetAttribute("innerText"); var videoControls = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)) .Until(driver => driver.FindElement(By.ClassName("ytp-chrome-controls"))); var action = new Actions(_driver); action.MoveToElement(videoControls).Perform(); // Get the total duration of the video var durationElement = _driver.FindElement(By.ClassName("ytp-time-duration")); var totalDuration = TimeSpan.Parse("00:" + durationElement.Text); while (true) { // Get the current time of the video var currentTimeElement = _driver.FindElement(By.ClassName("ytp-time-current")); var currentTime = TimeSpan.Parse("00:" + currentTimeElement.Text); // If the current time is equal to or greater than the total duration, refresh the page if (currentTime >= totalDuration) { _driver.Navigate().Refresh(); } // Wait for a second before checking the time again await Task.Delay(1000); } });This code continuously checks the current time of the video and compares it with the total duration. If the current time is equal to or greater than the total duration, it refreshes the page, effectively restarting the video. Please note that this will create an infinite loop, so you might want to add a condition to break out of the loop when needed. Also, replace
"ytp-time-duration"and"ytp-time-current"with the actual class names in your webpage if they’re different. Please ensure that your WebDriver has the necessary permissions to execute this script.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 10, 2024, 1:56 AM EDT
Hi Naimish
It run once then it gives error - String was not recognizes as a valid timespan
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 10, 2024, 3:34 AM EDT
The error message “String was not recognized as a valid TimeSpan” is thrown when the
TimeSpan.Parsemethod is unable to convert the string to aTimeSpanobject. This usually happens when the string format does not match the expected format for aTimeSpan.In your code, you are using
TimeSpan.Parse("00:" + currentTimeElement.Text)andTimeSpan.Parse("00:" + durationElement.Text). The error might be occurring because the text you are getting fromcurrentTimeElement.TextordurationElement.Textis not in the correct format.YouTube videos can be longer than an hour, and in such cases, the duration format is
HH:mm:ss. But if the video is less than an hour long, the format ismm:ss. So, when you are trying to parsemm:ssformat withTimeSpan.Parse("00:" + currentTimeElement.Text), it will throw an error becauseTimeSpan.Parseexpects the format to beHH:mm:ss.Here’s how you can fix this:
string FormatDuration(string duration) { var parts = duration.Split(':'); if (parts.Length == 2) // mm:ss format { duration = "00:" + duration; // convert to HH:mm:ss format } return duration; } // ... // Get the total duration of the video var durationElement = _driver.FindElement(By.ClassName("ytp-time-duration")); var totalDuration = TimeSpan.Parse(FormatDuration(durationElement.Text)); while (true) { // Get the current time of the video var currentTimeElement = _driver.FindElement(By.ClassName("ytp-time-current")); var currentTime = TimeSpan.Parse(FormatDuration(currentTimeElement.Text)); // ... }This
FormatDurationfunction checks if the duration is inmm:ssformat and if so, it converts it toHH:mm:ssformat.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 10, 2024, 3:40 AM EDT
Hi Naimish
It is giving same error. Below are the values
In currenTime it is showing 00:00:00
In totalDuration it is showing 00:00:07
Thanks