zoukankan      html  css  js  c++  java
  • c#使用selenium+Chromedriver参数配置

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

      

  • 相关阅读:
    LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别
    Linq扩展方法之All 、Any
    Linq扩展方法之Aggregate 对序列应用累加器函数
    C#开发的进化史
    C#选择文件、选择文件夹、打开文件(或者文件夹)
    XPath操作XML文档
    文档对象模型操作xml文档
    XML简介
    php获取某经纬度附近地点位置
    Yii中实例化类的四种方式
  • 原文地址:https://www.cnblogs.com/c-x-a/p/8494183.html
Copyright © 2011-2022 走看看