Answer the question
In order to leave comments, you need to log in
How to wait for YouTube selenium + phantom.js (c#) to load?
I'm trying to parse a list of youtube videos, accessing the page using Selenium + Phantom.js
IWebDriver webDriver = new PhantomJSDriver();
webDriver.Navigate().GoToUrl(@"https://www.youtube.com");
try
{
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
IWebElement firstResult = wait.Until(e => e.FindElement(By.CssSelector("ytd-rich-item-renderer")));
Console.WriteLine(firstResult.Text);
} catch(Exception e)
{
Console.WriteLine(e);
Console.WriteLine(webDriver.PageSource);
}
Console.ReadLine();
Answer the question
In order to leave comments, you need to log in
You can find everything from screenshots. Your search element is dynamic and is loaded separately. Moreover, you do not have a check for the number of elements.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
....
....
....
using (IWebDriver driver = new FirefoxDriver())
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("https://www.youtube.com");
wait.Until(web => {
var result = driver.FindElements(By.CssSelector("ytd-rich-item-renderer"));
return result.Count > 70;
});
var result = driver.FindElements(By.CssSelector("ytd-rich-item-renderer"));
foreach (var element in result)
{
Console.WriteLine(element.Text);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question