分享

《selenium2 python 自动化测试实战》(10)——下拉框和alert

 孟船长 2022-02-24

        先上代码:

# coding: utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep


# 去掉"Chrome正受到自动测试软件的控制。"
options = webdriver.ChromeOptions() options.add_argument('disable-infobars') driver = webdriver.Chrome(chrome_options=options) driver.get("https://www.baidu.com/")
# 鼠标移动到设置
ActionChains(driver).move_to_element(driver.find_element_by_xpath(".//*[@id='u1']/a[8]")).perform() sleep(2) driver.find_element_by_xpath(".//*[@id='wrapper']/div[6]/a[1]").click() sleep(2)
# 修改每页显示条数--1、定位到下拉框
driver.find_element_by_xpath(".//*[@id='nr']").click()
# 点击下拉框选项
driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click() sleep(2)
# 点击保存设置
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click() sleep(2) alert = driver.switch_to.alert
print alert.text alert.accept()

        对于下拉框最简单的处理就是两次定位——先定位下拉框->点击,弹出下拉框内容->定位下拉框里面的选项。

        上面代码后面的弹出框就是alert(提醒),可以先driver.switch_to.alert切换到alert,然后:

  • alert.text  ——打印alert内容

  • alert.accept()  ——点击确定

  • alert.dismiss()  ——点击取消(如果有)

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多