Top 10 Selenium WebDriver Commands Every Beginner Should Know
🚀 Top 10 Selenium WebDriver Commands Every Beginner Should Know (English)
If you're just starting with Selenium WebDriver in Python, here are the top 10 most essential commands you should learn. These commands will help you interact with browsers, find elements, fill forms, navigate pages, and much more.
✅ 1. Launch a Browser and Open a Website
python
Copy
Edit
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
🔹 Opens Chrome and navigates to the specified URL.
✅ 2. Find Elements
python
Copy
Edit
from selenium.webdriver.common.by import By
element = driver.find_element(By.ID, "username")
🔹 Common locators:
By.ID
By.NAME
By.CLASS_NAME
By.XPATH
By.CSS_SELECTOR
✅ 3. Send Text to Input Fields
python
Copy
Edit
element.send_keys("myusername")
🔹 Fills text input fields like username, email, or search bars.
✅ 4. Click on Buttons or Links
python
Copy
Edit
button = driver.find_element(By.XPATH, "//button[@type='submit']")
button.click()
🔹 Simulates a mouse click.
✅ 5. Clear Input Fields
python
Copy
Edit
input_field = driver.find_element(By.NAME, "email")
input_field.clear()
🔹 Clears any pre-filled or existing value.
✅ 6. Get Text from Elements
python
Copy
Edit
text = driver.find_element(By.TAG_NAME, "h1").text
print(text)
🔹 Useful for reading labels, messages, or titles.
✅ 7. Get Element Attributes
python
Copy
Edit
value = driver.find_element(By.ID, "username").get_attribute("value")
🔹 Access HTML attributes like href, src, value, placeholder, etc.
✅ 8. Wait for Elements (Explicit Wait)
python
Copy
Edit
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
🔹 Waits until the element is loaded before interacting.
✅ 9. Navigate Between Pages
python
Copy
Edit
driver.back()
driver.forward()
driver.refresh()
🔹 Use browser navigation like back, forward, and refresh.
✅ 10. Close Browser
python
Copy
Edit
driver.quit()
🔹 Closes all browser windows and ends the session.
🧠 Bonus: Full Sample Code
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
# Find and fill form
driver.find_element(By.NAME, "username").send_keys("user123")
driver.find_element(By.NAME, "password").send_keys("pass123")
driver.find_element(By.XPATH, "//button[@type='submit']").click()
# Close the browser
driver.quit()
🏁 Summary Table
Task Command
Open browser webdriver.Chrome()
Go to URL driver.get(url)
Find element driver.find_element(By.ID, "id")
Type text element.send_keys("text")
Click button element.click()
Clear field element.clear()
Get text element.text
Get attribute element.get_attribute("name")
Wait for element WebDriverWait(...).until(...)
Close browser driver.quit()
Learn Selenium JAVA Training in Hyderabad
Read More
Understanding XPath and CSS Selectors in Selenium
How to Handle Browser Pop-ups and Alerts in Selenium with Java
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment