C# dotnetcore2.0结合Selenium搜索网页

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp_Selenium
{
    class Program
    {
        static void Main(string[] args)
        {

            ChromeDriver cd = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory.ToString());
            
            cd.Navigate().GoToUrl("http://chart.icaile.com/sd11x5.php");            
            string text = cd.FindElementById("fixedtable").Text;
            
            cd.Quit();
            Console.WriteLine(text);
            Console.Read();
        }
    }
}

  

nuget安装

find-package selenium

install-package Selenium.WebDriver

install-package Selenium.WebDriver.ChromeDriver

 

使用headless chrome这样就没有界面。

using System;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp_Selenium
{
    class Program
    {
        static void Main(string[] args)
        {
            ChromeOptions op = new ChromeOptions();            
            op.AddArguments("--headless");            
            op.AddArguments("--window-size=1920,1080");            
            ChromeDriver cd = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory.ToString(),op);            
            cd.Navigate().GoToUrl("http://chart.icaile.com/sd11x5.php");            
            string text = cd.FindElementById("fixedtable").Text;            
            cd.Quit();
            Console.WriteLine(text);
            Console.Read();
        }
    }
}

  

 

下面是使用PhantomJS

nuget 安装> install-package Selenium.PhantomJS.WebDriver

 

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;
namespace ConsoleApp_Selenium
{
    class Program
    {
        static void Main(string[] args)
        {            
            PhantomJSOptions po = new PhantomJSOptions();            
            po.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0");                
            PhantomJSDriver cd = new PhantomJSDriver(AppDomain.CurrentDomain.BaseDirectory.ToString(), po);
            
            cd.Navigate().GoToUrl("http://chart.icaile.com/sd11x5.php");            
            string text = cd.FindElementById("fixedtable").Text;
            
            cd.Quit();
            Console.WriteLine(text);
            Console.Read();
        }
    }
}

  

转载于:https://www.cnblogs.com/coolyylu/p/8284662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值