C# Selenium操做指南,關閉黑色CMD窗口/禁用圖片/隱藏瀏覽器等

引用部分:
1.html

 


2.web

 


 
配置部分:chrome

ChromeDriverService driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;//關閉黑色cmd窗口

ChromeOptions options = new ChromeOptions();
// 不顯示瀏覽器
//options.AddArgument("--headless");
// GPU加速可能會致使Chrome出現黑屏及CPU佔用率太高,因此禁用
options.AddArgument("--disable-gpu");
// 假裝user-agent
options.AddArgument("user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1");
// 設置chrome啓動時size大小
options.AddArgument("--window-size=414,736");
// 禁用圖片
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);

IWebDriver webDriver = new ChromeDriver(driverService,options);
//若是查找元素在5S內尚未找到
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5); 
string url = "https://www.baidu.com";
webDriver.Navigate().GoToUrl(url);
 執行JS(將滾動條拉到底部):
((IJavaScriptExecutor)webDriver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
 獲取標籤(以多Class爲例):
ReadOnlyCollection<IWebElement> elements = webDriver.FindElements(By.CssSelector("[class='item goWork']"));
--------------------- 

 

http://www.javashuo.com/article/p-agijxwct-hv.html  不錯


原文:https://blog.csdn.net/PLA12147111/article/details/92000480

瀏覽器

相關文章
相關標籤/搜索