程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python automated test selenium (III) drop down selection box, warning box processing, page screenshot

編輯:Python

One 、 A drop-down box
selenium Provide
1、Select Three options are provided :
select_by_index(index) —— Through the order of options , The first is 0
select_by_value(value) —— adopt value attribute
select_by_visible_text(text) —— Text visible through options

2、Select There are four ways to deselect :

deselect_by_index(index)
deselect_by_value(value)
deselect_by_visible_text(text)
deselect_all()

3、Select Three attribute methods are provided to give us the necessary information :

options —— Provide a list of all options , These are all options WebElement Elements
all_selected_options —— Provide a list of all selected options , All of them are options WebElement Elements
first_selected_option —— Provide the first selected option , It is also the default value of the drop-down box
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
band = Select(driver.find_element(By.NAME, 'selDataSerialComBaudRate'))
band.select_by_visible_text(bandrate)

Two 、 Warning box handling
Use text/accept/dismiss/send_keys To operate .
switch_to_alert() # Positioning popup dialog
text() # Get dialog text value
accept() # It's like clicking " confirm "
dismiss() # It's like clicking " Cancel "
send_keys() # The input values , This alert and confirm No input dialog , So it can't be used here , So it can only be used in prompt here .

from selenium.webdriver.support.select import Select
alert = driver.switch_to_alert()
''' Add waiting time '''
time.sleep(2)
''' Get the contents of the warning dialog '''
print (alert.text) # Print warning dialog contents 
alert.accept() #alert Dialog belongs to warning dialog , We can only accept pop ups here 

3、 ... and 、 Screenshot of the page
driver.save_screenshot(“ Screenshot .png”)

import time
import re
import os
import _thread
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
if os.path.exists("232overip_config.jpg"):
os.remove("232overip_config.jpg")
driver.get_screenshot_as_file("232overip_config.jpg")

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved