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']"}
C#
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

Jayraj ChhayaRank #304 · 8.2K reputationPosted Aug 5, 2024, 5:36 AM EDT
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:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); var playButton = wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[contains(@class, 'ytp-play-button ytp-button') and @data-title-no-tooltip='Play']"))); playButton.Click();Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 6, 2024, 12:41 AM EDT
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:var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); var playButton = wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[contains(@class, 'ytp-play-button ytp-button') and @data-title-no-tooltip='Play']"))); playButton.Click();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:
driver.SwitchTo().Frame("iframeName");Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 6, 2024, 2:05 AM EDT
Hi Naimish
What is wrong in the current code. It runs second time for few seconds then the error comes
static async Task Main(string[] args) { await SingleVideo00(); } static async Task SingleVideo00() { var videos = new List { "hts://google.com" }; await Task.Run(async () => { const string XPATH_PLAY = "//button[contains(@class, 'ytp-play-button ytp-button') and @data-title-no-tooltip='Play']"; const string XPATH_MUTE = "//button[contains(@class, 'ytp-mute-button ytp-button')]"; const string XPATH_TIME = "//span[@class = 'ytp-time-duration']"; var service = ChromeDriverService.CreateDefaultService(); service.HideCommandPromptWindow = true; var options = new ChromeOptions(); using (var driver = new ChromeDriver(service, options)) { while (true) { foreach (var videoUrl in videos) { driver.Manage().Window.Size = new Size(1024, 768); driver.Navigate().GoToUrl(videoUrl); await Task.Delay(1000); var durationElement = driver.FindElement(By.XPath(XPATH_TIME)); var totalDuration = TimeSpan.Parse("00:" + durationElement.Text); var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 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; } await Task.Delay(1000); } } } } }); }Thanks
Thanks