Last active
October 24, 2022 10:13
-
-
Save SarahElson/72728d136e623eaf66cad5c524e6c46a to your computer and use it in GitHub Desktop.
How To Handle Errors and Exceptions In Selenium Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 protected]") | |
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