在C#中获取webdriver使用时出错

时间:2016-04-21 04:20:01

标签: c# selenium webdriver

您好我需要使用C#废弃网页。我是使用selenium webdriver。这是在Java中工作但C#抛出错误。这两个代码如下。

请让我知道我错在哪里

JAVA代码:---------------

                package com.tom.engine;

            import java.io.File;
            import java.io.FileWriter;
            import java.io.IOException;
            import java.util.Date;

            import javax.servlet.ServletException;
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            import org.apache.commons.logging.Log;
            import org.openqa.selenium.WebDriver;
            import org.openqa.selenium.firefox.FirefoxDriver;

            import com.gargoylesoftware.htmlunit.WebConsole.Logger;

            /**
             * Servlet implementation class MainEngine
             */
            public class MainEngine extends HttpServlet {
            private static final long serialVersionUID = 1L;


                /**
                 * @see HttpServlet#HttpServlet()
                 */
                public MainEngine() {
                    super();
                    // TODO Auto-generated constructor stub
                }

            /**
             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
            }

            /**
             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            WebDriver driver = new FirefoxDriver();
            String url=request.getParameter("debugUrl");
            System.out.println("Url to Parse====>"+url);
            driver.get(url);
            String parsedPage=driver.getPageSource();
            driver.close();
            driver.quit();
            //creating file here
            Date date=new Date();
            String filePath="C:\\Users\\mainak\\Documents\\"+date.getTime()+".txt";
            try{
            File file=new File(filePath);
            //create file here
            if(file.exists()){
            System.out.println("File already exists");
            }else{
            System.out.println("Generated File:"+filePath);
            file.createNewFile();
            FileWriter fw=new FileWriter(file);
            fw.write(parsedPage);
            fw.close();
            }
            }catch(IOException ie){
            ie.printStackTrace();
            }
            }

            }

Html源代码的C#代码聚集------

                using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Net;
            using System.IO;
            using System.Threading.Tasks;
            using OpenQA.Selenium;
            using OpenQA.Selenium.Support.UI;
            using OpenQA.Selenium.IE;
            using OpenQA.Selenium.Chrome;
            using OpenQA.Selenium.Firefox;


            namespace selenium_instance_test
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                          IWebDriver driver1 = new InternetExplorerDriver(@"E:\DotNet_dev\tutorial\IEDriverServer_x64_2.53.0");

                                String surl="https://www.flightradar24.com/data/aircraft/vt-ppa";
                                Console.WriteLine("Url to Parse====>"+surl);


                              //  wrGETURL = WebRequest.Create(surl);
                               // wrGETURL = driver1.Url;
                               //Stream objStream;
                               //objStream=wrGETURL.GetResponse().GetResponseStream();
                            driver1.Navigate().GoToUrl(surl);

                            //IList<IWebElement> elements = driver1.FindElements(By.ClassName("fa fa-plane"));
                            //IWebElement Nametest = driver1.FindElement(By.Name("DATE"));

                            String surl1=driver1.PageSource();

                           // driver1.WaitUntilLoaded();
                       // String surl1 = (string)js.ExecuteScript("return surl.title");


                            String surl1 = driver1.PageSource(surl);
                                System.IO.File.WriteAllText(@"E:\DotNet_dev\tutorial\WriteLines.txt", surl1);

                                driver1.Quit();

                    }[Error for page source ][1]

                }
            }

Error screenshot 1

2 个答案:

答案 0 :(得分:1)

在C#中

_driver.Navigate().GoToUrl(Key);
//Page Source 
var result = _driver.PageSource;

答案 1 :(得分:0)

首先,您必须在安全设置中启用保护模式。详见this。然后,您必须将PageSource()更改为PageSource。请参阅下面的代码。

string surl = "https://www.flightradar24.com/data/aircraft/vt-ppa";
Console.WriteLine("Url to Parse====>" + surl);
driver1.Navigate().GoToUrl(surl);
string surl1 = driver1.PageSource;
driver1.Quit();