Last active
March 5, 2024 05:04
-
-
Save mols3131d/6e810f0ddaf7b56d0f5cf43292ec4ebf to your computer and use it in GitHub Desktop.
Selenium4
This file contains 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
|
This file contains 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
from selenium import webdriver | |
from selenium.common.exceptions import WebDriverException | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.select import Select | |
from selenium.webdriver.support.ui import WebDriverWait | |
def close_new_tabs(driver: webdriver): | |
tabs = driver.window_handles | |
while len(tabs) != 1: | |
driver.switch_to.window(tabs[1]) | |
driver.close() | |
tabs = driver.window_handles | |
driver.switch_to.window(tabs[0]) | |
def select_dropdown(driver: webdriver.Chrome, xpath, text): | |
element = driver.find_element(By.XPATH, xpath) | |
select = Select(element) | |
select.select_by_visible_text(text) | |
def input_box(driver: webdriver.Chrome, xpath, text): | |
element = driver.find_element(By.XPATH, xpath) | |
element.clear() | |
element.send_keys(text) | |
def accept_alert(driver: webdriver.Chrome): | |
wait = WebDriverWait(driver, 10) | |
wait.until(EC.alert_is_present()) | |
try: | |
alert = driver.switch_to.alert | |
alert.accept() | |
except: | |
pass | |
def switch_frame( | |
driver: webdriver.Chrome, | |
ftrame_xpath: str or list, | |
frame_init=True, | |
wait_frame=True, | |
): | |
if frame_init: | |
driver.switch_to.default_content() | |
if isinstance(ftrame_xpath, str): | |
ftrame_xpath = [ftrame_xpath] | |
if wait_frame: | |
wait = WebDriverWait(driver, 10) | |
wait.until( | |
EC.presence_of_element_located((By.XPATH, f"""{ftrame_xpath[0]}""")) | |
) | |
for xpath in ftrame_xpath: | |
frame = driver.find_element(By.XPATH, xpath) | |
driver.switch_to.frame(frame) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment