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

      

  • 相关阅读:
    Realtime crowdsourcing
    maven 常用插件汇总
    fctix
    sencha extjs4 command tools sdk
    首次吃了一颗带奶糖味的消炎药,不知道管用不
    spring mvc3 example
    ubuntu ati driver DO NOT INSTALL recommand driver
    yet another js editor on windows support extjs
    how to use springsource tools suite maven3 on command
    ocr service
  • 原文地址:https://www.cnblogs.com/c-x-a/p/8494183.html
Copyright © 2011-2022 走看看