我没有得到IFrame中的Html并由javascript生成

时间:2016-04-27 07:18:00

标签: c# selenium web

我正在使用selenium和Htmlaglity pack here是网站

这是我的代码

IWebDriver driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver("D:/Testing/");
driver.Navigate().GoToUrl(suburl);
Thread.Sleep(50000);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1000));
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(driver.PageSource);

1 个答案:

答案 0 :(得分:1)

您必须在获取PageSource之前切换到iframe。请参阅以下代码:

IWebDriver driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver("D:/Testing/");
driver.Navigate().GoToUrl(suburl);
Thread.Sleep(50000);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1000));
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(driver.PageSource);

driver.SwitchTo().Frame(driver.FindElement(By.Id("blockrandom")));
HtmlAgilityPack.HtmlDocument iframeDoc = new HtmlAgilityPack.HtmlDocument();
iframeDoc.LoadHtml(driver.PageSource);