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
Ramco RamcoPosted Aug 6, 2024, 6:05 AM
Hi Naimish
What is wrong in the current code. It runs second time for few seconds then the error comes
Thanks
Thanks
Naimish MakwanaPosted Aug 6, 2024, 4:41 AM
Sure, here are some possible solutions to your problem:
WebDriverWaitin combination withExpectedConditionsto wait until the element is present. Here’s how you can do it:Correct the XPath: Make sure the XPath is correct. You can test the XPath in your browser’s developer tools to ensure it’s selecting the right element.
Switch to the correct iframe: If the element is inside an iframe, you’ll need to switch to that iframe before trying to find the element. Here’s how you can switch to an iframe:
Thanks
Jayraj ChhayaPosted Aug 5, 2024, 9:36 AM
The error "no such element" in Selenium WebDriver typically occurs when the WebDriver is unable to find an element on the web page using the specified locator. In this case, the error is related to the XPath selector used to locate a play button element.
To resolve this issue, you can try the following steps:
Verify the XPath selector: Double-check the XPath selector used to locate the play button element. Ensure that it is correct and matches the structure of the web page.
Wait for the element to be present: Sometimes, the element may not be immediately available when the page loads. You can use explicit waits to wait for the element to be present before interacting with it. Here's an example: