Java + Selenium + chrome をちょっと触ってみるまでがつまずき放題だった。
放題。好きなように躓けばいいんだ。
むかーしむかしにpythonで遊んで以来さわってなかったselenium。
windows10 + STS(Java)でSelenium動かしてみたかったんだけどなかなかたどりつけなかった。
qiita.com
を参考にさせてもらっていたんだけど。
・そもそも記事はたぶんMac環境
・3年以上前の記事でいまとだいぶ勝手が違うきがする。
というようなことが要因でうまくいかず。
とはいえ記事のおかげで300倍ぐらい解決が早かった気がするので感謝の意を示しつつ、
30分ぐらいかなーってのが2時間かからないぐらいでたどり着けたので記録としてのこす。
目次
libないんじゃが・・・
とりあえずseleniumが必要。ということでダウンロードサイトで拾ってくる。
Downloads | Selenium
が、想定よりもライブラリが少なかった。なんでや。
ぶっ飛ばして手順どおりやったけどコードが全然ライブラリ足りなくてビルドができなかった。
"Stable"って安定版ってことだとおもったのに。こんなかんじ。
gyazo.com
そのあとMavenで引っ張り出したのがこんな感じ。
gyazo.com
なのでだいぶ足りない様子。
記事内サンプルコードは@TestもついてるのでJUnitもいるっぽいが。現状はMavenでも搭載されてないんじゃないかな。
Mavenがんばれ。
ということで公式からダウンロードしてきてもライブラリが足りてないっぽいので(読めてないだけ説が濃厚なんですが)Mavenプロジェクトに切り替えてMavenで落とすことにした。
こちらを参考に。とくに困ることはなく。
qiita.com
Maven Repositoly か公式からDependencyは取ってくればいいと思うけど。
公式からとってくるときはバージョンちうい。
こんな感じでバージョンのところが不定になっている。
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.X</version> </dependency> 引用元:[https://www.selenium.dev/documentation/getting_started/installing_selenium_libraries/:title]
pom.xmlつくったことないぜ。な人はdependencyそのままはっつけてエラーになって混乱すると思う(混乱した)。
dependenciesで囲むことも忘れないようにちうい。こんな感じになるYO
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>selenium.practice</groupId> <artifactId>seleniumPractice</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> </dependencies> </project>
ということで無事ライブラリゲットした。
サンプル実装コード
公式の実装コードはこちら。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.WebDriverWait; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; import java.time.Duration; public class HelloSelenium { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); try { driver.get("https://google.com/ncr"); driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER); WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3"))); System.out.println(firstResult.getAttribute("textContent")); } finally { driver.quit(); } } } 引用元: [https://www.selenium.dev/documentation/]
java15でさわっていたけど、WebDriverWaitの第2引数がDuration対応してなくてlongっぽいのでそこはなおした。
コードはfirefoxのドライバなので、chromeように直すと以下のようになる。
WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, 10L); try { driver.get("https://google.com/ncr"); driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER); WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3"))); System.out.println(firstResult.getAttribute("textContent")); } finally { driver.quit(); }
これだけだとchromedriverのパスがわからなくてエラーになる。
仕組みはいまいち理解していないがChromeDriverのインスタンスを生成する前にパスを指定する必要がある。
System.setProperty("webdriver.chrome.driver", "driver/chromedriver92.exe");
こんな感じで。上だと実行環境にdriverフォルダほってそこに置いてるイメージになる。
gyazo.com
これね。ドライバが2つあるのはChromeのバージョンとあってなくて別のバージョン用のをもってきたから。1つでOK。
全部足すとこんな感じに。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.WebDriverWait; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; public class HelloSelenium { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "driver/chromedriver92.exe"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, 10L); try { driver.get("https://google.com/ncr"); driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER); WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3"))); System.out.println(firstResult.getAttribute("textContent")); } finally { driver.quit(); } } }
おわり。
おわり。楽勝だと思ったら楽勝じゃなかった。ぱわぁが足りない。
先人の知恵は偉大だなぁ。非常に助かる。ありがたや。