Three kinds of switching in web automation-window switching

1. Window switching

  • Get the total number of open window handles and return a list

    handles=driver.window_handles

  • Switch to the latest window handle

    driver.switch_to.window(handles[-1])

  • The operation code is as follows:
. 1  Import Time
 2  from Selenium Import the webdriver
 . 3  from selenium.webdriver.support Import expected_conditions AS EC
 . 4  from selenium.webdriver.support.wait Import WebDriverWait
 . 5  from selenium.webdriver.common.by Import By
 . 6  # window switching operation 
. 7 Driver = the webdriver .chrome ()
 8  # to access a web page 
9 driver.get ( " https://www.baidu.com " )
 10  driver.maximize_window ()
11 driver.find_element_by_id ( ' kw ' ) .send_keys ( " lemon class " )
 12 driver.find_element_by_id ( ' su ' ) .click ()
 13 WebDriverWait (driver, 30) .until (EC.visibility_of_element_located ((By.XPATH, ' // a [contains (text (), "吧 _ 百度 贴 吧")] ' )))
 14 driver.find_element_by_xpath ( ' // a [contains (text (), "吧 _ 百度 贴 吧")] ' ) .click ()
 15 the time.sleep (0.5) # wait for 0.5 seconds, click OK pages are open 
16  # obtain a handle to open the browser, returns a list of 
17 the handles =driver.window_handles
18 print(handles)
19 driver.switch_to.window(handles[-1])
20 # 打印当前句柄
21 print(driver.current_window_handle)
22 # :Args:    driver.switch_to.window(window_name)
23 #          - window_name: The name or window handle of the window to switch to.
24 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='signstar_wrapper']//a[@title='签到']")))#等待元素出现
25 driver.find_element_by_xpath("//div[@id='signstar_wrapper']//a[@title='签到']").click()
26 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,"TANGRAM__PSP_11__footerULoginBtn")))
27 driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
28 driver.find_element_by_id('TANGRAM__PSP_5__closeBtn').click()

2. Determine whether the new window has been opened for use 

  EC.new_window_is_opened(current_handles)

  The operation code is as follows:

  

. 1  Import Time
 2  from Selenium Import the webdriver
 . 3  from selenium.webdriver.support Import expected_conditions AS EC
 . 4  from selenium.webdriver.support.wait Import WebDriverWait
 . 5  from selenium.webdriver.common.by Import By
 . 6  # window switching operation 
. 7 Driver = the webdriver .chrome ()
 8  # to access a web page 
9 driver.get ( " https://www.baidu.com " )
 10  driver.maximize_window ()
11 driver.find_element_by_id ( ' kw ' ) .send_keys ( " lemon class " )
 12 driver.find_element_by_id ( ' su ' ) .click ()
 13 WebDriverWait (driver, 30) .until (EC.visibility_of_element_located ((By.XPATH, ' // a [contains (text () , " _ Baidu bar bar ')] ' )))
 14  # total handle before acquisition window open a new page 
15 handles = driver.window_handles
 16 driver.find_element_by_xpath ( ' // a [ contains (text (), "吧 _ 百度 贴 吧")] ' ) .click ()
 17 time.sleep (0.5) #Wait 0.5 seconds, click OK pages are open 
18  # waiting for a new page opens 
19 WebDriverWait (Driver, 20 ) .until (EC.new_window_is_opened (the Handles))
 20  # obtain a handle to open the browser, returns a list of 
21 the Handles = Driver .window_handles
 22 is  Print (handles)
 23 is  # switched to the new handle 
24 driver.switch_to.window (handles [-1 ])
 25  # Print handle 
26 is  Print (driver.current_window_handle)
 27  # : the Args: driver.switch_to.window ( window_name) 
28  #           -window_name: The name or window handle of the window to switch to. 
29 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='signstar_wrapper']//a[@title='签到']")))#等待元素出现
30 driver.find_element_by_xpath("//div[@id='signstar_wrapper']//a[@title='签到']").click()
31 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,"TANGRAM__PSP_11__footerULoginBtn")))
32 driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
33 driver.find_element_by_id('TANGRAM__PSP_5__closeBtn').click()

 

Guess you like

Origin www.cnblogs.com/wsk1988/p/12696810.html