selenium + python learning - pop-up box

1, alert alert box

To Baidu search settings, for example, after conducting search settings click "Save Settings" button, pop-up alert dialog box, as shown below:

 

 

 Implementation code:

from selenium import webdriver
import time as t
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
t.sleep(3)
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").send_keys(Keys.ENTER)
# 显式等待,待元素定位
WebDriverWait (Driver, 10) .until (ec.presence_of_element_located ((By.ID, "kW"))) 
# Print (" drop-down box to select the number of the latest is: ", select.get_attribute ( 'value')) 
# text positioning 
select (select).select_by_visible_text ( "10 per")
# Hover 
Mouse = driver.find_element_by_name ( "tj_settingicon") 
ActionChains (Driver) .move_to_element (Mouse) .perform () 
driver.find_element_by_link_text ( 'search setting') .click () 
# # Implicit wait 
# driver.implicitly_wait ( 10) 
t.sleep (. 5) 
# drop-down box positioned 
sELECT = driver.find_element_by_name ( "NR") 
# # indexes positioned in 
# the select (sELECT) .select_by_index (2) 
# Print ( "drop-down box to select the latest number of the is: ", select.get_attribute ( 'value')) 
# positioning value to 
# the select (sELECT) .select_by_value (" 10 ") 
T.SLEEP (4) 
# determine whether the selected is_selected ()
print ( "drop-down box to select the number of the latest is:", select.get_attribute ( 'value')) 
t.sleep (. 3) 
driver.find_element_by_xpath ( '// * [@ ID = ". 5-SE-Setting"] / label [. 1] '.) the Click () 
S = driver.find_element_by_xpath (' // * [@ ID = ". 5-SE-Setting"] / label [. 1] ') is_selected (). 
IF S == False: 
    Driver .find_element_by_xpath ( '// * [@ ID = ". 5-SE-Setting"] / label [. 1]') the Click (). 
    Print (S) 
the else: 
    Print (True) 
driver.find_element_by_css_selector ( "# gxszButton> A. prefpanelgo. ") the Click () 
# Alert elastic frame 
print (" alert popup text information:. {0} "format ( driver.switch_to.alert.text)) # popup text information acquired 
driver.switch_to.alert .accept () 
driver.quit ()

  ps: without the driver.switch_to.alert (), otherwise there will be TypeError

accept () to confirm

dismiss () denote canceled

2, confirm check box

display:

 

 Implementation code:

from selenium import webdriver
import time


url = 'file:///C:/Users/yl8/Desktop/链接.html'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(4)
# confirm操作
driver.find_element_by_id("confirm").click()
time.sleep(2)
t = driver.switch_to.alert
print(t.text)
t.dismiss()

3, prompt dialog

Types of:

 

Implement the test code:

from selenium import webdriver
import time


url = 'file:///C:/Users/yl8/Desktop/链接.html'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(4)
# prompt操作
driver.find_element_by_id("prompt").click()
time.sleep(2)
t = driver.switch_to.alert
print(t.text)
t.send_keys("hello selenium")
t.accept()

  

 

 

 

Guess you like

Origin www.cnblogs.com/yuer02/p/12625551.html