LoginSignup
1
0

More than 1 year has passed since last update.

Selenium備忘録

Posted at

メモせずにいつも同じことを調べてしまうので、備忘的に随時追記。

個別のブラウザモジュール(Chromium)を指定

Chromiumのexeファイルを以下のように指定すれば可能。

ChromeOptions options = new ChromeOptions();
// Chromiumを指定
options.setBinary("./lib/chromium-win/chrome.exe");

ブラウザの枠外にある要素を枠内に持ってくる

javascriptで。scrollIntoViewのパラメータ:falseで下寄せ、trueで上寄せ。targetXpathは対象要素のXpath。

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", driver.findElement(By.xpath(targetXpath)));

新しいタブを開いてリンクを開く

targetXpathは対象リンクのXpath。

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
driver.findElement(By.xpath(targetXpath)).sendKeys(selectLinkOpeninNewTab);

要素の存在確認

targetXpathは対象要素のXpath。

// 要素が出現(見える)まで10秒待機
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(targetXpath)));
if (0 < driver.findElements(By.xpath(targetXpath)).size()) {
	// findElementsのサイズで判定させる
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0