专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

c#使用selenium+Chromedriver参数配置

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:40:43       共计:3631 浏览
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome;   using OpenQA.Selenium.Support.UI; using OpenQA.Selenium; //添加引用-在程序集中添加System.Drawing using System.IO; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             string baitai_id="006";             string word = "python";             string savepath = "666.html";           var getsource=SearchYahoo(savepath,word, baitai_id);         }                    public static string SearchYahoo(string SavePath,string word,string baitai_id)         {             ChromeOptions options = new ChromeOptions();             //Set the http proxy value, host and port.             //Set the proxy to the Chrome options             var proxy = new Proxy();             proxy.Kind = ProxyKind.Manual;             proxy.IsAutoDetect = false;             proxy.HttpProxy = "localhost:8080";             proxy.SslProxy = "localhost:8080";             options.Proxy = proxy;               options.AddArgument("ignore-certificate-errors");             //options.AddArgument("--headless");             options.AddArgument("--disable-gpu");             string ua = null;             string url = @"https://search.yahoo.co.jp/";             var spua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25";             var pcua = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36";                 switch (baitai_id)             {                 case "002":                     ua = pcua;                     url = @"https://www.yahoo.co.jp/";                     break;                 case "004":                     ua = spua;                     break;                 case "006":                     ua = spua;                     break;                 default:                     break;             }               options.AddArgument(string.Format("--user-agent={0}", ua));             List<String> tagNmaeList = new List<string>(); ;             var driver = new ChromeDriver(options);                          string Source=null;             try             {                     //002的换用另外程序                 driver.Navigate().GoToUrl(url);                 var inputNode = driver.FindElementByXPath(@"//input[@name='p']");                 Thread.Sleep(3000);                 inputNode.SendKeys(word);                 var search = driver.FindElementByXPath(@"//input[@type='submit']");                 Thread.Sleep(2000);                 search.Click();                 Thread.Sleep(5000);                 Source = driver.PageSource;                                 }             catch (Exception ex)             {                                               }             finally             {                // driver.Close();                                 driver.Quit();                                            }             System.IO.File.WriteAllText(SavePath, Source, System.Text.Encoding.UTF8);             return Source;             }     }         }
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:记录:c#中使用Selenium之二 整页截图 | ·下一条:用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有