Three switching modes in selenium & three waiting modes

preface

ui automatic testing is to simulate manual operation through code, and operate browser pages by clicking, dragging and inputting. Our commonly used web automatic testing tool is selenium; At the same time, in our daily operation, we need to create links, load pages, switch windows and other situations, but the execution of the code is very fast. How can we achieve the operations we need? At this time, we need to operate through switching, waiting and other methods.

1, Three switching of selenium

There are three switching modes: window switching, iframe switching and alert pop-up switching

1. Window switching

Gets the current handle driver current_ window_ handle
 Get all current window handles driver window_ handles 
Switch to the latest window driver switch_ to. window(driver.window_handles[-1])
Switch to the first window driver switch_ to. window(driver.window_handles[0])
# Window switching

from selenium import webdriver

driver = webdriver.Chrome()  # Get browser
driver.implicitly_wait(10)   # Invisible waiting, waiting for elements to load
driver.get('http://www.baidu.com ') # access url address

elem = driver.find_element('id','kw')    # Element positioning
elem.send_keys('haha')  # input
elem.submit()  # Submit

driver.find_element_by_partial_link_text('Baidu Translate').click()
print(driver.window_handles)  # Get all window handles
print(driver.current_window_handle)  # Gets the current window handle

driver.switch_to.window(driver.window_handles[-1])  # Switch to the latest window
time.sleep(2)
print(driver.current_window_handle) # Gets the current window handle
print(driver.title)  # Print browser title

2. iframe switching

How do you judge that you are in the iframe interface? Write an xpath expression through F12, ctrl + F to determine that the expression is unique, but you still can't successfully obtain the element. At this time, check whether there are iframe words in the upper and lower elements you want to locate to confirm:

There are three basic operations of iframe:

1) Get the driver through the index index switch_ to. frame(0)
2) If there is a name element, get it through the name element and directly pass in the value to the driver switch_ to. frame("iframeResult")
3) Get through iframe WebElement  
  locator=['id','kw']
  driver.switch_to.frame(locator)
# iframe switching

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.csdn.net/')
driver.switch_to.frame(0)   # Switch to iframe
elem = driver.find_element('xpath',"//div[@id='app']//div[@id='7574'] ") # locate elements in iframe
print(elem)

3. alert pop-up Toggle

my_alert = driver.switch_to.alert
 my_alert.accept() # confirm
 my_ alert. Disass() # cancel
elem = driver.find_element('xpath','//h2')
elem.click()
my_alert = driver.switch_to.alert   # Switch to alert pop-up
my_alert.accept()  # confirm
my_alert.dismiss()  #  cancel
elem1 = driver.find_element('id','kw')  # After operating the alert pop-up box, locate the element

2, Three waiting modes of selenium

1. Forced wait (hard wait)

Force to wait for n seconds and add it every time you use it
time.sleep(2)

2. Invisible waiting (Intelligent waiting)

The unit is s. The table returns at the fastest speed in n seconds, but it can only be used to wait for elements. After starting the browser, it only needs to be set once
driver.implicity_wait(10)

3. Explicit waiting

Each time you use it, you need to start it. You can not only wait for elements, but also wait for others. When the timeout is found, an error will be reported.
           In addition, the waiting time is dead and can generally be called time Instead of sleep (2),
wait = WebDriverWait(driver,10)
wait.until (expected_conditions.title_contains('xxx ')) # wait until the title contains XXX

locator = ['id','kw']
elem = wait.until(expected_conditions.element_to_be_clickable(locator)) # wait for the element to be clicked
 elem = wait.until(expected_conditions.presence_of_element_located(locator)) # wait for an element to load
 elem = wait.until(expected_conditions.visibility_of_element_located(locator)) # wait for the element to be visible
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()
driver.implicitly_wait(10)  # Recessive wait returns the result as quickly as possible within 10s, but can only wait for elements. It is set globally once
driver.get('http://www.baidu.com')

elem = driver.find_element('id','kw')
elem.send_keys('haha')
elem.submit()
# time.sleep(3)   # Forced wait, must wait three seconds
# print(driver.title)  # The page element we want cannot be located because it has not been loaded. This item needs to wait
# driver.quit()

wait = WebDriverWait(driver,10)  # Explicit wait, wait condition until the title contains' haha 'text
wait.until(expected_conditions.title_contains('haha'))
print(driver.title)
driver.quit()

3, Three switching waiting modes of selenium

1. Window switch wait

wait = WebDriverWait(driver,4) # initialize the wait
 all_handles = driver.window_handles # get all windows before clicking
 wait.until(expected_conditions.new_window_is_opened(all_handles)) # wait explicitly for a new window to appear
 driver.switch_to.window(driver.window_handles[-1]) # need to switch to the latest window manually
# Window switch wait

from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
import time


driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://www.baidu.com')
elem = driver.find_element('id','kw')
elem.send_keys('haha')
elem.submit()

all_handles = driver.window_handles   # Get all windows before clicking
driver.find_element_by_partial_link_text('Baidu Translate').click()
wait = WebDriverWait(driver,2)
wait.until(expected_conditions.new_window_is_opened(all_handles))    # Wait for the new window to appear by explicit waiting
driver.switch_to.window(driver.window_handles[-1])   # Switch to the latest window
time.sleep(2)
print(driver.title)

2. iframe switch wait

wait = WebDriverWait(driver,4) # initializes the wait
 wait.until(expected_conditions.frame_to_be_available_and_switch_to_it(0)) # wait and automatically switch to iframe
# iframe switching
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.csdn.net/')

wait = WebDriverWait(driver,4)
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it(0))  # Wait and switch to iframe
elem = driver.find_element('xpath',"//div[@id='app']//div[@id='7574']")
print(elem)

3. alert switch wait

wait = WebDriverWait(driver,4) # initializes the wait
 wait.until(expected_conditions.alert_is_present()) # wait and automatically switch to iframe

summary

The above is a simple operation of the three switches and three waits. I have a one-sided understanding. I'll record it here first.

Recent mood:

I feel like I'm waiting every day. It's really meaningless. Hey, I hate myself now. Solve it quickly and end the current situation. I feel like adjusting my mind and fighting!!

End, sprinkle flowers ~ ~!!!!!!

Keywords: Python Selenium

Added by aussie_clint on Fri, 21 Jan 2022 00:41:41 +0200