【问题标题】:Visual Studio C# Selenium: cannot find Driver [duplicate]Visual Studio C# Selenium:找不到驱动程序 [重复]
【发布时间】:2017-04-18 12:32:36
【问题描述】:

我运行这段代码并不断地在一行中遇到问题:“IWebDriver Driver = new OpenQA.Selenium.Chrome.ChromeDriver();”当我以这种方式运行它而不是指向 ChromeWebDriver 时,我得到:“当前目录或 PATH 环境变量的目录中不存在 chromedriver.exe 文件。”

当我给出 ChromeWebDriver 的路径时,我得到:“错误 CS1009:无法识别的转义序列”

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
  using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
 IWebDriver Driver = new OpenQA.Selenium.Chrome.ChromeDriver
 ("C:\Users\stand\Documents\
 Visual Studio 2015\Projects\WindowsFormsApplication2\ChromeWebDriver.exe");
             Driver.Navigate().GoToUrl("http://dallas.craigslist.org/");
        IWebElement Element = Driver.FindElement(By.Name("query"));
        Element.SendKeys("cx-5");

       }
   }
}

【问题讨论】:

    标签: c# string filepath


    【解决方案1】:

    使用

    IWebDriver Driver = new OpenQA.Selenium.Chrome.ChromeDriver (@"C:\Users\stand\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\ChromeWebDriver.exe");
    

    反斜杠在字符串文字中具有特殊含义。您可以复制所有反斜杠,或者如本例所示,通过在开头添加 @ 符号将字符串文字变为“逐字字符串文字”。在逐字字符串文字中,反斜杠没有特殊含义


    另一种解决方案意味着您不必手动指定 exe 文件的完整路径,可以找到 in this answer:将 ChromeWebDriver.exe 文件添加到项目并选择将其自动复制到与你自己的application.exe。

    【讨论】:

    • 感谢您的回答!
    【解决方案2】:

    您需要转义路径中的反斜杠或使用字符串文字。

    IWebDriver Driver = new ChromeDriver (@"C:\Users\stand\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\ChromeWebDriver.exe");

    【讨论】:

    • 谢谢。这行得通!
    猜你喜欢
    • 2016-12-16
    • 2018-04-17
    • 2019-06-23
    • 2018-05-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多