关键词搜索

源码搜索 ×
×

c#使用Selenium相关总结

发布2023-02-14浏览389次

详情内容

1、在 C# 中使用 Selenium WebDriver 执行 JavaScript

  1. IWebDriver driver; // assume assigned elsewhere
  2. IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
  3. string title = (string)js.ExecuteScript("return document.title");

2、载取网页可视区域图片

  1. public static void PageScreenshot(string url,string path)
  2. {
  3. ChromeDriver driver = null;
  4. try
  5. {
  6. ChromeOptions options = new ChromeOptions();
  7. options.AddArguments("headless", "disable-gpu");
  8. driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
  9. driver.Navigate().GoToUrl(url);
  10. var screenshot = (driver as ITakesScreenshot).GetScreenshot();
  11. screenshot.SaveAsFile(path);
  12. }
  13. catch (Exception ex)
  14. {
  15. logger.Error(ex.Message+Environment.NewLine+ex.StackTrace);
  16. }
  17. finally
  18. {
  19. if (driver != null)
  20. {
  21. driver.Close();
  22. driver.Quit();
  23. }
  24. }
  25. }

3、载取网页有滚动条网页长图

  1. public static void PageScreenshot(string url,string path)
  2. {
  3. ChromeDriver driver = null;
  4. try
  5. {
  6. ChromeOptions options = new ChromeOptions();
  7. options.AddArguments("headless", "disable-gpu");
  8. driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
  9. driver.Navigate().GoToUrl(url);
  10. string width = driver.ExecuteScript("return document.body.scrollWidth").ToString();
  11. string height = driver.ExecuteScript("return document.body.scrollHeight").ToString();
  12. driver.Manage().Window.Size = new System.Drawing.Size(int.Parse(width), int.Parse(height)); //=int.Parse( height);
  13. var screenshot = (driver as ITakesScreenshot).GetScreenshot();
  14. screenshot.SaveAsFile(path);
  15. }
  16. catch (Exception ex)
  17. {
  18. logger.Error(ex.Message+Environment.NewLine+ex.StackTrace);
  19. }
  20. finally
  21. {
  22. if (driver != null)
  23. {
  24. driver.Close();
  25. driver.Quit();
  26. }
  27. }
  28. }

未完.......

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载