1、在 C# 中使用 Selenium WebDriver 执行 JavaScript
- IWebDriver driver; // assume assigned elsewhere
- IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
- string title = (string)js.ExecuteScript("return document.title");
2、载取网页可视区域图片
- public static void PageScreenshot(string url,string path)
- {
- ChromeDriver driver = null;
- try
- {
- ChromeOptions options = new ChromeOptions();
- options.AddArguments("headless", "disable-gpu");
- driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
- driver.Navigate().GoToUrl(url);
- var screenshot = (driver as ITakesScreenshot).GetScreenshot();
- screenshot.SaveAsFile(path);
- }
- catch (Exception ex)
- {
- logger.Error(ex.Message+Environment.NewLine+ex.StackTrace);
- }
- finally
- {
- if (driver != null)
- {
- driver.Close();
- driver.Quit();
- }
- }
- }
3、载取网页有滚动条网页长图
- public static void PageScreenshot(string url,string path)
- {
- ChromeDriver driver = null;
- try
- {
- ChromeOptions options = new ChromeOptions();
- options.AddArguments("headless", "disable-gpu");
- driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
- driver.Navigate().GoToUrl(url);
- string width = driver.ExecuteScript("return document.body.scrollWidth").ToString();
- string height = driver.ExecuteScript("return document.body.scrollHeight").ToString();
- driver.Manage().Window.Size = new System.Drawing.Size(int.Parse(width), int.Parse(height)); //=int.Parse( height);
- var screenshot = (driver as ITakesScreenshot).GetScreenshot();
- screenshot.SaveAsFile(path);
- }
- catch (Exception ex)
- {
- logger.Error(ex.Message+Environment.NewLine+ex.StackTrace);
- }
- finally
- {
- if (driver != null)
- {
- driver.Close();
- driver.Quit();
- }
- }
- }
未完.......