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")