Contenu connexe Similaire à Selenium 触ってみよう (20) Plus de Oda Shinsuke (20) Selenium 触ってみよう7. 環境構築 (Visual Studio)
Visual Studio NuGet からインストール
Package Manage Console(NuGet) から
Support は便利だからいれてます
Install-Package Selenium.WebDriver
Install-Package Selenium.Support
install-package Selenium.IEDriver
今日のコードはこちら
https://github.com/OdaShinsuke/20150411_ASPNET
10. using System;
using OpenQA.Selenium.IE;
using OpenQA.Selenium;
class Program {
static void Main(string[] args) {
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.bing.com");
Console.WriteLine(driver.Title);
Console.ReadKey();
driver.Quit();
}
}
Bing にいってみる
13. using System;
using OpenQA.Selenium.IE;
using OpenQA.Selenium;
class Program {
static void Main(string[] args) {
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.bing.com");
IWebElement element = driver.FindElement(By.Name("q"));
element.SendKeys("セレニウム ウェブドライバー");
Console.ReadKey();
driver.Quit();
}
}
テキストボックスに文字を入力
16. using System;
using OpenQA.Selenium.IE;
using OpenQA.Selenium;
class Program {
static void Main(string[] args) {
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.bing.com");
IWebElement elementByName = driver.FindElement(By.Name("q"));
elementByName.SendKeys("セレニウム ウェブドライバー");
IWebElement elementById = driver.FindElement(By.Id("sb_form_q"));
elementById.SendKeys(" ID で取ったお");
IWebElement elementByCss = driver.FindElement(
By.CssSelector("b_searchbox"));
elementByCss.Clear();
elementByCss.SendKeys("CssSelector で");
Console.ReadKey();
driver.Quit();
}
}
色んな取り方をしてみる
18. エレメント の プロパティ
基本 get だけ
Displayed
Enabled
Location
Selected
チェックボックスや Select の Option
Size
TagName
Text
タグに挟まれたテキストのこと
テキストボックスの値じゃない!
19. class Program {
static void Main(string[] args) {
var driver = new InternetExplorerDriver();
try {
driver.Navigate().GoToUrl("http://www.bing.com");
var txt条件 = driver.FindElementByName("q");
txt条件.SendKeys("SQLWorld");
txt条件.Submit();
Thread.Sleep(3000); // 次の画面に遷移するまで待つ
var lbl件数 = driver.FindElementByClassName("sb_count");
Console.WriteLine(lbl件数.Text);
Console.ReadKey();
} finally {
driver.Quit();
}
}
}
検索してみる
21. Wait
WebDriverWait が良く使われる
コンストラクタでタイムアウト時間指定
Until メソッドに Func を渡し条件指定
Func は、既定の実装が幾つかある
ExpectedConditions
Func を自前で実装も可
wait.Until(d => d.Title == "Microsoft - Bing");
IWait<IWebDriver> wait =
new WebDriverWait(driver, new TimeSpan(0, 0,10));
wait.Until(ExpectedConditions.TitleIs("Microsoft - Bing"));
22. class Program {
static void Main(string[] args) {
var driver = new InternetExplorerDriver();
try {
driver.Navigate().GoToUrl("http://www.bing.com");
var txt条件 = driver.FindElementByName("q");
txt条件.SendKeys("SQLWorld");
txt条件.Submit();
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
wait.Until(ExpectedConditions.TitleIs("SQLWorld - Bing"));
var lbl件数 = driver.FindElementByClassName("sb_count");
Console.WriteLine(lbl件数.Text);
Console.ReadKey();
} finally {
driver.Quit();
}
}
}
Wait 使って検索してみる
24. class Program {
static void Main(string[] args) {
var driver = new InternetExplorerDriver();
try {
driver.Navigate().GoToUrl("http://sqlworld.org/event/20150425/");
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
wait.Until(ExpectedConditions.TitleIs("SqlWorld :: SQLWorld★大阪#30"));
driver.GetScreenshot().SaveAsFile(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ @"sqlworld.png",
ImageFormat.Png);
Console.ReadKey();
} finally {
driver.Quit();
}
}
}
スクリーンショット をとってみる
25. class Program {
static void Main(string[] args) {
var driver = new InternetExplorerDriver();
try {
driver.Navigate().GoToUrl("http://sqlworld.org/event/20150425/");
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
wait.Until(ExpectedConditions.TitleIs("SqlWorld :: SQLWorld★大阪#30"));
driver.FindElementByName("name").SendKeys("あなたのお名前");
driver.FindElementByName("email").SendKeys("あなたのE-Mail");
driver.FindElementByName("commentText").SendKeys(@"宜しくお願いします!
懇親会:参加");
Console.ReadKey();
driver.FindElementByName("Post Comment").Submit();
Console.ReadKey();
} finally {
driver.Quit();
}
}
}
実践編!