1) 前期准备
在官网的下载页面找到C#版本的weddriver下载下来(Selenium Client & WebDriver Language Bindings)
对于IE浏览器你还需要下载The Internet Explorer Driver Server
下载上述包文件后,解压缩。
2) 创建C# 控制台项目并添加对Webdriver.dll的引用。开始编写第一个脚本吧。下面是官网给出的示例。
3) using OpenQA.Selenium;
4) using OpenQA.Selenium.Firefox;
5)
6) // Requires reference to WebDriver.Support.dll
7) using OpenQA.Selenium.Support.UI;
8)
9) class GoogleSuggest
10) {
11) static void Main(string[] args)
12) {
13) // Create a new instance of the Firefox driver.
14)
15) // Notice that the remainder of the code relies on the interface,
16) // not the implementation.
17)
18) // Further note that other drivers (InternetExplorerDriver,
19) // ChromeDriver, etc.) will require further configuration
20) // before this example will work. See the wiki pages for the
21) // individual drivers at http://code.google.com/p/selenium/wiki
22) // for further information.
23) IWebDriver driver = new FirefoxDriver();
24)
25) //Notice navigation is slightly different than the Java version
26) //This is because 'get' is a keyword in C#
27) driver.Navigate().GoToUrl("http://www.google.com/");
28)
29) // Find the text input element by its name
30) IWebElement query = driver.FindElement(By.Name("q"));
31)
32) // Enter something to search for
33) query.SendKeys("Cheese");
34)
35) // Now submit the form. WebDriver will find the form for us from the element
36) query.Submit();
37)
38) // Google's search is rendered dynamically with JavaScript.
39) // Wait for the page to load, timeout after 10 seconds
40) WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
41) wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
42)
43) // Should see: "Cheese - Google Search"
44) System.Console.WriteLine("Page title is: " + driver.Title);
45)
46) //Close the browser
47) driver.Quit();
48) }
49) }作者: 樱花季节 时间: 2013-5-9 17:21
好帖,学习了作者: langhai5212 时间: 2013-5-17 16:36
有什么问题,可以提问,尽量回答啊作者: martin_lc 时间: 2013-5-29 12:35 回复 3#langhai5212