Created
September 1, 2017 17:51
-
-
Save nddipiazza/231d38a4b5aacbaaa54d00ed4dd253b0 to your computer and use it in GitHub Desktop.
Headless firefox (without xvfb) example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mozilla.example; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.firefox.FirefoxBinary; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxOptions; | |
import java.util.concurrent.TimeUnit; | |
public class HeadlessFirefoxSeleniumExample { | |
public static void main(String [] args) { | |
FirefoxBinary firefoxBinary = new FirefoxBinary(); | |
firefoxBinary.addCommandLineOptions("--headless"); | |
System.setProperty("webdriver.gecko.driver", "/home/ndipiazza/Desktop/geckodriver"); | |
FirefoxOptions firefoxOptions = new FirefoxOptions(); | |
firefoxOptions.setBinary(firefoxBinary); | |
FirefoxDriver driver = new FirefoxDriver(firefoxOptions); | |
try { | |
driver.get("http://www.google.com"); | |
driver.manage().timeouts().implicitlyWait(4, | |
TimeUnit.SECONDS); | |
WebElement queryBox = driver.findElement(By.name("q")); | |
queryBox.sendKeys("headless firefox"); | |
WebElement searchBtn = driver.findElement(By.name("btnK")); | |
searchBtn.click(); | |
WebElement iresDiv = driver.findElement(By.id("ires")); | |
iresDiv.findElements(By.tagName("a")).get(0).click(); | |
System.out.println(driver.getPageSource()); | |
} finally { | |
driver.quit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment