How to type the Enter/Return key in Selenium

Java

In Java, use the org.openqa.selenium.Keys enumeration.

import org.openqa.selenium.Keys
driver.findElement(By.id("myID")).sendKeys(Keys.RETURN);

Python

In Python, use the Keys enumeration from selenium.webdriver.common.keys.

from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("myID").send_keys(Keys.RETURN)

C#

In C#, use the OpenQA.Selenium.Keys enumeration.

using OpenQA.Selenium.Keys
driver.FindElement(By.Id("myID")).SendKeys(Keys.Return);

How to use Selenium headless

In Java and Python, this merely requires using a headless browser like PhantomJS.

Java

import org.openqa.selenium.phantomjs.PhantomJSDriver;
Webdriver driver = new PhantomJSDriver();

Python

from selenium import webdriver
driver = webdriver.PhantomJS()

C#

The PhantomJS classes are no longer supported in C#, so you need to use Firefox or Chrome in headless mode instead.

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
IWebDriver driver;
FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--headless");
driver = new FirefoxDriver(options);

How to take a screenshot in Selenium

Java

In Java, cast the Webdriver to a TakeScreenshot and then call its takeScreenshotAs() method.
This will create a File object, which you can then copy to a real disk file.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

Python

In Python, use the save_screenshot() method of the webdriver.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.google.com/')
driver.save_screenshot("c:\\tmp\\screenshot.png")

C#

In C#, cast the driver to a ITakesScreenshot and then call its GetScreenshot method.
You can then call SaveAsFile() on the returned Screenshot object.

Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(@"C:\tmp\screenshot.png", System.Drawing.Imaging.ImageFormat.Png);