Selenium in three switching web (B) and the iframe window Alert (pop) switch

A, web iframe window switch the window

First, iframe html is inside the html page.

If you encounter an element to be operated in iframed which is required for this operation.

Steps:

Element 1) determines whether to operate in an iframe current, can be determined using a positioning method element;

2) If an element is present is judged. It determines the need to switch to the iframe.

3) handover method:

1 driver.switch_to.frame()

Parameter supports three ways to determine which switch to a iframe:

. 1  # . 1) from 0 iframe subscript 
2 driver.switch_to.frame (. 3 )
 . 3  
. 4  # 2) attribute name iframe element 
. 5 driver.switch_to.frame ( " login_frame_qq " )
 . 6  
. 7  # . 3) webElment this iframe element. 
. 8  
. 9 driver.switch_to.frame (driver.find_element_by_xpath ( ' // iframes [@ name = "login_frame_qq"]))

Specific implementation process:

1) Open the pages visited;

2) The element positioning operation, into the iframe has a page;

3) Operation to pages which have iframe, and subsequent operations, elements in this iframe to find and operate;

4) switch to the iframe window into which the next generation html

driver.switch_to.frame ( "iframe window _ Name")

5) positioned in the operating element firame window;

6) back to the default html page which - the first generation
driver.switch_to.default_content ()

7) back to the level iframe - the previous generation
driver.switch_to.parent_frame ()

Example: for reference only.

import time
from  selenium import  webdriver
from selenium.webdriver.common.by import By
from  selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 1、打开访问腾讯课堂
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://ke.qq.com/")

# 2, according to the login button xpath locate the current page, click Login to enter the QQ login window 
LOC = (By.XPATH, ' // A [@ the above mentioned id = "js_login"] ' )
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc))
driver.find_element_by_xpath('//a[@id="js_login"]').click()

# 3, forced to wait for one second 
the time.sleep (. 1 )

# 4, to find QQ login button in the new pop, click to enter the QQ login account password input box 
loc2 = (By.XPATH, ' // i [@ class = "font-i-icon QQ"] ' )
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc2))
driver.find_element_by_xpath('//i[@class="icon-font i-qq"]').click()

# 5, QQ account login window 
loc3 = (By.XPATH, ' // iframes. [@ Name = "login_frame_qq"] ' )
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc3))
driver.find_element_by_xpath('//iframe[@name="login_frame_qq"]').click()

# 6, switching to a new iframe window 
driver.switch_to.frame ( " login_frame_qq " )

# 7, the QQ login window navigate to the QQ account information 
LOC4 = (By.XPATH, ' // * [@ the above mentioned id = "img_out_1xxxx"] ' )
WebDriverWait(driver,20).until(EC.visibility_of_element_located(loc4))
driver.find_element_by_xpath('//*[@id="img_out_1xxxx"]').click()

# 9, exit the session 
the time.sleep (10 )
driver.quit()

 

Two, Alert (pop) of the switching window Web

Many times we encounter pop, and must be in order to operate on the page pop operation. Popups higher priority than any of the elements on the page;

Here operated in this type of window.

step:

1) Click the trigger operation, trigger popups appear;

1 driver.find_element_by_id("press").click()

2) Wait pop occurs;

3) The operation is switched to pop;

1 driver.switch_to.alert

4) The pop-off, the processing operation for the next page

1 print(alert.text)
2 alert.accept

 

Example:

. 1  "" " 
2  Alert class
 . 3  " "" 
. 4  Import Time
 . 5  
. 6  from   Selenium Import   the webdriver
 . 7  
. 8 Driver = webdriver.Chrome ()
 . 9 driver.get ( " E: \ Workspace \ Python \ the Web \ class_05_0307 \ MyHtml.html " )
 10  
. 11  # click action appears to trigger playing block 
12 is driver.find_element_by_id ( " Press " ) .click ()
 13 is  # wait occurs pop 
14 the time.sleep (. 1 )
 15  
16  # handover pop
. 17 Alert = driver.switch_to.alert
 18 is  
. 19  # will pop off, the processing for the next page 
20 is  Print (alert.text)
 21 is  alert.accept ()
 22 is  
23 is  # Close Session 
24 the time.sleep (. 5 )
 25 Driver .quit ()

 

Guess you like

Origin www.cnblogs.com/forayepy/p/12446095.html