WebDriver 加载网页获取元素、对某个元素截图示例
一:WebDriver使用
1、首先下载chrome插件:chromedriver.exe,保存到某地址例如c:11
下载地址:http://chromedriver.storage.googleapis.com/index.html
2、通过NuGet 安装组件
通过NuGet安装
3、代码使用示例
driver.Navigate().GoToUrl("https://passport.baidu.com/?getpassindex&tt=1597054938536&gid=00C700C-A457-4CCF-8588-F118FFF70829&tpl=mn&u=https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3DUTF-8"); string phone = "133258"; var search = driver.FindElement(By.Id("account")); //search.Click(); search.SendKeys(phone); var btn = driver.FindElement(By.Id("submit")); Thread.Sleep(1000); try { btn.Click(); } catch (Exception ex) { } Thread.Sleep(1000); try { btn.Click(); } catch (Exception ex) { } //获取指定元素 var imgElemet = driver.FindElement(By.ClassName("vcode-spin-img")); // getImgFile(driver, imgElemet.Location.X - 8, imgElemet.Location.Y); //截图 Screenshot screenshot = ((ITakesScreenshot)imgElemet).GetScreenshot(); //保存图片 var dirpath = @"C:\B2BImge"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } Listlist = new List (); string filepath = @"C:\B2BImge\" + Guid.NewGuid().ToString() + ".png"; screenshot.SaveAsFile(filepath, ScreenshotImageFormat.Png);//保存图片
参考文献:
https://blog.csdn.net/qewyv/article/details/105694486
https://blog.csdn.net/weixin_43474597/article/details/114136427