Basic Selenium Commands Every Beginner Should Know

 ๐Ÿงช Basic Selenium Commands Every Beginner Should Know

Selenium is a powerful tool used for automating web browsers. If you're just starting out with Selenium (especially in Python or Java), here are some of the most important and commonly used commands:


✅ 1. Launching the Browser

Python:


python

Copy

Edit

from selenium import webdriver


driver = webdriver.Chrome()  # Opens Chrome browser

Java:


java

Copy

Edit

WebDriver driver = new ChromeDriver();  // Opens Chrome browser

✅ 2. Opening a Website

python

Copy

Edit

driver.get("https://www.google.com")

java

Copy

Edit

driver.get("https://www.google.com");

✅ 3. Maximizing the Window

python

Copy

Edit

driver.maximize_window()

java

Copy

Edit

driver.manage().window().maximize();

✅ 4. Finding Elements

You can find elements by ID, Name, Class, Tag, XPath, CSS, etc.


python

Copy

Edit

driver.find_element("id", "searchBox")

java

Copy

Edit

driver.findElement(By.id("searchBox"));

✅ 5. Typing into Input Fields

python

Copy

Edit

element = driver.find_element("name", "username")

element.send_keys("my_username")

java

Copy

Edit

WebElement element = driver.findElement(By.name("username"));

element.sendKeys("my_username");

✅ 6. Clicking Buttons

python

Copy

Edit

driver.find_element("id", "submit").click()

java

Copy

Edit

driver.findElement(By.id("submit")).click();

✅ 7. Getting Text from an Element

python

Copy

Edit

text = driver.find_element("tag name", "h1").text

print(text)

java

Copy

Edit

String text = driver.findElement(By.tagName("h1")).getText();

System.out.println(text);

✅ 8. Waiting for Elements (Explicit Waits)

python

Copy

Edit

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 10)

element = wait.until(EC.presence_of_element_located((By.ID, "username")))

java

Copy

Edit

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));

✅ 9. Closing the Browser

Close the current tab:


python

Copy

Edit

driver.close()

java

Copy

Edit

driver.close();

Quit all tabs and end session:


python

Copy

Edit

driver.quit()

java

Copy

Edit

driver.quit();

✅ 10. Taking a Screenshot

python

Copy

Edit

driver.save_screenshot("screenshot.png")

java

Copy

Edit

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshot, new File("screenshot.png"));

๐Ÿง  Bonus Tips

Always use waits (explicit or implicit) to make tests more reliable.


Use XPath or CSS Selectors for complex element selection.


Handle exceptions like NoSuchElementException for stability.

Learn Selenium Python Training in Hyderabad

Read More

Writing Your First Selenium Script in Python

Installing Selenium WebDriver in Python: A Quick Guide

Why Use Python for Selenium Automation?

What is Selenium? A Beginner’s Guide for Python Developers

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Tosca for API Testing: A Step-by-Step Tutorial

Working with Tosca Parameters (Buffer, Dynamic Expressions)