Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Created May 30, 2024 07:15
Show Gist options
  • Save SarahElson/efb73954fadd51a45bac80802133f0c2 to your computer and use it in GitHub Desktop.
Save SarahElson/efb73954fadd51a45bac80802133f0c2 to your computer and use it in GitHub Desktop.
Selenium sendKeys(): A Complete Guide
const { setupDriver } = require("./web-driver");
async function performSearch() {
const driver = await setupDriver();
try {
// Navigate to Google's homepage
await driver.get("<https://www.google.com/>");
// Find the search input element and send the search query
const searchInput = await driver.findElement(By.name("q"));
await searchInput.sendKeys("Selenium JavaScript");
// Submit the search using the Enter key
await searchInput.sendKeys(Key.ENTER);
// Wait for the search results page to load
await driver.wait(until.titleContains("Selenium JavaScript"), 5000);
// Print the page title
const pageTitle = await driver.getTitle();
console.log("Page title:", pageTitle);
} catch (error) {
console.error("Error occurred:", error);
} finally {
// Quit the driver after the test is finished
await driver.quit();
}
}
// Call the function to run the test
performSearch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment