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

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);
            }
        }
    }
}

  

posted @ 2018-05-30 10:33  公众号python学习开发  阅读(809)  评论(0编辑  收藏  举报