• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#使用selenium截图,操作dom元素

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
//添加selenium的引用
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static PhantomJSDriver driver { get; set; }

        static void Main(string[] args)
        {
            //PhantomJSDriverService driverService = PhantomJSDriverService.CreateDefaultService();
            ////driverService.IgnoreSslErrors = true; //"any" also works
            //driverService.HideCommandPromptWindow = true;
            //driverService.ProxyType = "none";
            //driverService.SslProtocol = "any";
            //driverService.MaxDiskCacheSize = 1000;
            //driverService.DiskCache = true;
            //driverService.WebSecurity = false;
            //PhantomJSOptions phs = new PhantomJSOptions();
            //phs.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
       
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--headless");
            options.AddArgument("--nogpu");
            List<String> tagNmaeList = new List<string>();;
            //using (driver = new PhantomJSDriver(driverService, phs))
            //{
            using (var driver = new ChromeDriver(options))
                {
                driver.Manage().Window.Maximize();
                driver.Navigate().GoToUrl(@"https://qiita.com/ttake/items/2e470462f8d90e76e996");
                Thread.Sleep(5000);
                string source = driver.PageSource;
                Console.WriteLine(driver.Url);
                 var html = driver.FindElement(By.TagName("html"));
                 try
                 {
                     var div = driver.FindElement(By.Id("global-nav"));

                 }
                 catch (NoSuchElementException)
                 {
                     Console.WriteLine("找不到该元素"); ;
                 } 
                 IJavaScriptExecutor js = (IJavaScriptExecutor)driver;


                 var divnode = driver.FindElement(By.ClassName("highlight"));

                 var bkgimg= js.ExecuteScript("arguments[0].style.getPropertyValue('background-image')", divnode);
                 if (bkgimg==null)
                 {
                    var outerhtml = divnode.GetAttribute("outerHTML");  //outerhtml
                    var outerText = divnode.GetAttribute("outerText");
                    var innerText = divnode.GetAttribute("innerText");
                    var children = divnode.GetAttribute("children");
                    var textContent = divnode.GetAttribute("textContent");
                    var childElementCount = divnode.GetAttribute("childElementCount");
                  }

                 var image=driver.FindElement(By.ClassName("it-Header_authorImage"));
                 
                 var width = image.Size.Width;
                 var height = image.Size.Height;
                 var elem = js.ExecuteScript("return document.defaultView.getComputedStyle(arguments[0])", image);
                 
                 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
                 var dirpath = @"D:\temp";
                 if (!Directory.Exists(dirpath))
                 {
                 
                    Directory.CreateDirectory(dirpath);
                 
                 }
                 screenshot.SaveAsFile(@"D:\temp\seleniumScreenshot.png", ScreenshotImageFormat.Png);
            }
        }
    }
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#枚举之间的转换发布时间:2022-07-18
下一篇:
asp.net利用Cookie实现免登陆(c#)发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap