c#使用selenium截图,操作dom元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 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); } } } } |
本文作者:一起来学python
本文链接:https://www.cnblogs.com/c-x-a/p/9109538.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步