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
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 MakwanaPosted Apr 10, 2024, 8:26 AM
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:
This
FormatDurationfunction now also checks if the duration is null or empty and if so, it returns “00:00:00”.Ramco RamcoPosted Apr 10, 2024, 7:40 AM
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
Naimish MakwanaPosted Apr 10, 2024, 7:34 AM
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:
This
FormatDurationfunction checks if the duration is inmm:ssformat and if so, it converts it toHH:mm:ssformat.Thanks
Ramco RamcoPosted Apr 10, 2024, 5:56 AM
Hi Naimish
It run once then it gives error - String was not recognizes as a valid timespan
Thanks
Naimish MakwanaPosted Apr 10, 2024, 5:01 AM
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:
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