Every virtual machine product comes with some basic sample code. With these samples you can learn the most common browser operations. There are three language bindings pre-installed: Java, Javascript and Python. Those are the most popular languages in Selenium development.

The below sample code is for Chrome on a Ubuntu server without a graphical user interface. Sample code for other OS and browser can be found on the virtual machines.

Init webdriver:

Before we start the Chrome browser, we specify some options. The first options tells Chrome to run in Headless mode, this means without a GUI.
Afterwards we start Chrome via Chromedriver. Selenium 3 does not interact directly with the browser itself.
When it is ready, we tell the driver to wait 5 seconds in case an element can not be found after the page loaded. This can be the case for dynamic content of website. At last, we set the browser X and Y dimension in pixels.

options = COptions()
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument("--lang=en-GB") #remove this line if you want to set the browser locale to the region you launched this instance
driver = webdriver.Chrome(executable_path=r"/home/ubuntu/chromedriver", chrome_options=options)
driver.implicitly_wait(5) #general timeout for waiting on dynamic web elements
driver.set_window_size(1500, 700)
System.setProperty("", "/home/ubuntu/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--lang=en-GB"); //remove this line if you want to set the browser locale to the AWS region you launched this instance

WebDriver driver = new ChromeDriver(chromeOptions);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //general timeout for waiting on dynamic web elements
driver.manage().window().setSize(new Dimension(1500,700)); //set window size
const screen = {
  width: 1500,
  height: 700

var driver = new Builder().
    setChromeOptions(new chrome.Options().headless().windowSize(screen)).

Navigate to URL:

We now navigate to this webpage using the driver.get() method.

await driver.get('');

Take a screenshot:

Let us take a screenshot to proove we are on the page. This also works in headless browser!

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("/home/ubuntu/java/screenshot.png"));
driver.takeScreenshot().then(function(data) {
        writeScreenshot(data, 'screenshot1.png');
function writeScreenshot(data, name) {
  var fs = require('fs');
  var screenshotPath = '/home/ubuntu/nodejs/';
  fs.writeFileSync(screenshotPath + name, data, 'base64');

Click on element via XPath:

Before we can click an element on the page, we need to locate it. There are different methods to achieve this, the most common way is via XPath.
There are dozens of good tools to get the XPath of elements, Chrome has also one built-in.

Below you find a screenshot of the popular ChroPath plugin:

Learn more about XPath here

element = driver.find_element_by_xpath("//a[@id='java-tab1']") #relative XPath
element = driver.find_element_by_xpath("//main/section/div/ul/li[1]/a") #absolute XPath
WebElement element = driver.findElement(By.xpath("//a[@id='java-tab1']")); //relative XPath;
element = driver.findElement(By.xpath("//main/section/div/ul/li[1]/a")); //absolute XPath;
await driver.findElement(By.xpath('//a[@id="java-tab1"]')).click(); //relative XPath
await driver.findElement(By.xpath('//main/section/div/ul/li[1]/a')).click(); //absolute XPath

Click on element via class name:

Another way to locate web elements is to use their class names. The problem is, that a class name is generally used more than once on a page, so we get a list of web elements instead of a single one.

elements = driver.find_elements_by_class_name("nav-link") #this returns a list of all nav-link elements
elements[1].click() #click on second element in list, which is 'Products' in topbar
element = driver.findElements(By.className("nav-link")).get(1); //#click on second element in list, which is 'Documentation' in topbar;
await driver.findElements({className:'nav-link'}).then(async function(elements) {
        // click on second element in array, which is 'Products' in topbar
        await elements[1].click();

Using browser back button:

This sample shows you how to use the back button of the browser. There is no built in function for that in Selenium, so we are going to use JavaScript.

JavascriptExecutor jse = (JavascriptExecutor)driver;
await driver.executeScript('window.history.go(-1);');

Scroll down to specific element:

Same goes for scrolling a specific element into view. We first specify the element, then we use JavaScript to scroll that element into view.

element = driver.find_element_by_xpath("//input[@id='text-field']")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element = driver.findElement(By.xpath("//input[@id='text-field']"));
jse.executeScript("arguments[0].scrollIntoView(true);", element);
await driver.findElement(By.xpath("//input[@id='text-field']")).then(async function(element){
        await driver.executeScript("arguments[0].scrollIntoView(true);", element);

Send string to input field and open popup:

Now we type in some text into a text field. Afterwards we click on the button which opens a small popup.

element.send_keys("Sent from Selenium Headless Browser")
element = driver.find_element_by_xpath("//button[contains(text(),'Click me!')]")
element.sendKeys("Sent from Selenium");
element = driver.findElement(By.xpath("//button[contains(text(),'Click me!')]"));;
await driver.findElement(By.xpath("//input[@id='text-field']")).sendKeys("Sent from Selenium Headless Browser");
await driver.findElement(By.xpath("//button[contains(text(),'Click me!')]")).click();

Get text of element:

The last code block shows you, how you can pull out the visible text of an element.

element = driver.find_element_by_xpath("//div[@id='modalbody-text']")
text = element.text
element = driver.findElement(By.xpath("//div[@id='modalbody-text']"));
String text = element.getText();
await driver.findElement(By.xpath("//div[@id='modalbody-text']")).getText().then((text) => {
      console.log("Text of popup: "+text);