如何通过c#获取网站的页面源类似的检查员?

时间:2017-08-30 06:26:27

标签: c# selenium selenium-chromedriver

我想用c#获取网站的所有源代码。 我尝试使用此代码,获取源代码但未完成网站代码。 对于Exapmle:site 用于显示价格(قیمت)其他选项(保证(گارانتی),颜色(رنگ)) ,使用脚本 ajax 。此信息存在于检查中,但页面源中不存在。

使用此:

vs2013 v12 update4

Selenium.WebDriver.3.0.1\lib\net40\WebDriver.dll

试试此代码:

       link_Pagesource = link_Pagesource.Replace("http://", "https://");
        HttpWebRequest URL_pageSource = (HttpWebRequest)WebRequest.Create(link_Pagesource);
        URL_pageSource.Timeout = 360000;
        URL_pageSource.ReadWriteTimeout = 360000;
 using (WebResponse MyResponse_PageSource = URL_pageSource.GetResponse())
      {
           str_PageSource = new StreamReader(MyResponse_PageSource.GetResponseStream(), 
          System.Text.Encoding.UTF8);
          pagesource1 = str_PageSource.ReadToEnd();

        }

 IWebDriver _driver;
    _driver = new ChromeDriver();

   _driver.Navigate().GoToUrl(link_Pagesource);
    pagesource1 = _driver.PageSource;
   _driver.Close();
   _driver.Quit();

和:

               driver5 = new ChromeDriver();
                driver5.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(500));
                IJavaScriptExecutor js = (IJavaScriptExecutor)driver5;
                string title = (string)js.ExecuteScript("return document.body.innerHTML;");

                string title2 = (string)js.ExecuteScript("document.documentElement.outerHTML;");

                IList<IWebElement> all = new List<IWebElement>();
                IList<IWebElement> divtag = driver5.FindElements(By.XPath("//div"));
                IList<IWebElement> labelstag = driver5.FindElements(By.XPath("//label"));

                String[] allText = new String[all.Count];
                int i = 0;
                foreach (IWebElement element in divtag)
                {
                    allText[i++] = element.Text;
                }

                String[] allText1 = new String[all.Count];
                int y = 0;
                foreach (IWebElement element in labelstag)
                {
                    allText[y++] = element.Text;
                }

对我来说,所有数据都很重要,在load js或ajax中存在重要数据。

没有在pagesource获得此网站的代码。

pic of site,selected by draq important data for me

pic for price(قیمت)** and other options(guaranty(گارانتی),color(رنگ))

elements and div for u

此链接是源完成保存但不能使用c#访问:

http://localhost:5049/session/0fba2e005ffd1efeb39a9a999bfbb2d5/source

错误超时60秒

1 个答案:

答案 0 :(得分:1)

请尝试以下方法: -

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.body.innerHTML;");

OR

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("document.documentElement.outerHTML;");

只需打印标题字符串变量

即可

使用以下代码打印所有div和标签

IList<IWebElement> divtag = driver.FindElements(By.XPath("//div"));
IList<IWebElement> labelstag = driver.FindElements(By.XPath("//label"));

foreach (IWebElement element in divtag)
{
    element.Text;
}

foreach (IWebElement element in labelstag)
{
    element.Text;
}

希望它会对你有所帮助:)。