Alert box processing
In WebDriver processing the generated alert JavaScript, Confirm very simple and prompt, which would be used to locate a method switch_to.alert alert / confirm / prompt, and then operate using text / accept / dismiss / send_keys like.
-
text: return a text message alert / confirm / prompt in.
-
accept (): accept the existing alert box.
-
dismiss (): the dissolution of the existing alert box.
-
send_keys (keysToSend): to send a text alert box. keysToSend: will send a text to the alert box.
For chestnut, opening pop-up window is provided Baidu search through the tool can not be located, this time by switch_to_alert () method takes the pop. code show as below:
from selenium.webdriver Import Chrome from selenium.webdriver.common.action_chains Import ActionChains Import Time Driver = Chrome ( " : C \ Program Files (x86) \ Google \ Chrome \ the Application \ chromedriver.exe " ) (driver.implicitly_wait 10 ) Driver .get ( ' http://www.baidu.com ' ) # hover to "set" link link = driver.find_element_by_link_text ( ' set ' ) ActionChains (Driver) .move_to_element (link) .perform () # open Search set up driver.find_element_by_link_text ( " Search Settings " ) .click () the time.sleep ( 5 ) # Save Settings driver.find_element_by_link_text ( " Save Settings " ) .click () the time.sleep ( 2 ) # accept the warning box driver.switch_to.alert .accept () driver.quit ()
Obtained by switch_to_alert () method box warning on the current page, and use the accept () method accepts a warning box.
Select the drop-down box
A drop-down box Select class
Sometimes we encounter the drop-down box, WebDriver Select provides classes to handle the drop-down box
Import module: from the Select Import selenium.webdriver.support.select
Select View source module, is how to define the various methods Select package. The specific method is shown below:
Second, the conventional method Select class
1, Select provides three methods to select an item
- select_by_index # index given by
- value of the positioning value by select_by_value #
- By locating text values select_by_visible_text #
Precautions:
index index is from "0";
value是option标签的一个属性值,并不是显示在下拉框中的值;
visible_text是在option标签中间的值,是显示在下拉框的值;
2、Select提供了三种返回options信息的方法
- options # 返回select元素所有的options
- all_selected_options # 返回select元素中所有已选中的选项
- first_selected_options # 返回select元素中选中的第一个选项
注意事项:
这三种方法的作用是查看已选中的元素是否是自己希望选择的:
options:提供所有选项的元素列表;
all_selected_options:提供所有被选中选项的元素列表;
first_selected_option:提供第一个被选中的选项元素;
3、Select提供了四种取消选中项的方法
- deselect_all # 取消全部的已选择项
- deselect_by_index # 取消已选中的索引项
- deselect_by_value # 取消已选中的value值
- deselect_by_visible_text # 取消已选中的文本值
注意事项:
在日常的web测试中,会经常遇到某些下拉框选项已经被默认选中,这种时候就需要用到这里所说的四种方法;
下面我们来举个栗子。看看如何使用,打开百度首页设置,修改设置中显示条数并保存设置,如下代码:
from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.implicitly_wait(10)
driver.get('http://www.baidu.com')
# 鼠标悬停至“设置”链接
link = driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(link).perform()
sleep(1)
# 打开搜索设置
driver.find_element_by_link_text("搜索设置").click()
sleep(2)
# 搜索结果显示条数
sel = driver.find_element_by_xpath("//select[@id='nr']")
Select(sel).select_by_value('50') # 显示50条
sleep(2)
driver.find_element_by_link_text("保存设置").click()
# 接受警告框
driver.switch_to.alert.accept()
driver.quit()
以上就是Select类的使用方法demo,其他方法的使用,后续结合项目使用实践总结。