Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulmr/d2d8b46d2440c5f9c907863460448362 to your computer and use it in GitHub Desktop.
Save rahulmr/d2d8b46d2440c5f9c907863460448362 to your computer and use it in GitHub Desktop.
How To Handle Errors and Exceptions In Selenium Python
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
ch_capabilities = {
'LT:Options' : {
"user" : "<username>",
"accessKey" : "<accesskey>",
"build" : "Errors and Exceptions Test on Chrome",
"name" : "Errors and Exceptions Test on Chrome",
"platformName" : "Windows 10"
},
"browserName" : "Chrome",
"browserVersion" : "102.0",
}
def test_ecommerceplayground_staleelement():
# LambdaTest Profile username
user_name = "<username>"
# LambdaTest Profile access_key
app_key = "<accesskey>"
# Remote Url to connect to our instance of LambdaTest
remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
# creating an instance of Firefox based on the remote url and the desired capabilities
ch_driver = webdriver.Remote(
command_executor=remote_url, desired_capabilities = ch_capabilities)
ch_driver.get('https://ecommerce-playground.lambdatest.io/index.php?route=account/login')
emailElement = ch_driver.find_element(By.ID, "input-email")
passwordElement = ch_driver.find_element(By.ID, "input-password")
emailElement.send_keys("email@gmail.com")
ch_driver.find_element(By.XPATH, "//input[@type='submit']").click()
try:
passwordElement.send_keys("password")
except StaleElementReferenceException:
print('StaleElementReferenceException handled')
passwordElement = ch_driver.find_element(By.ID, "input-password")
passwordElement.send_keys("password")
ch_driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment