Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项

学习目的:


 

  了解WebDriver的高级应用

 

正式步骤:


 

测试Python3代码

# -*-  coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import unittest
import time
import traceback


class WebdriverAPI(unittest.TestCase):
    def setUp(self):
        # 每个用例都执行,在单个用例运行前执行
        #打开浏览器
        self.driver = webdriver.Chrome()

    def tearDown(self):
        #每个用例都执行,在单个用例运行后执行
        #退出浏览器
        self.driver.quit()

    def test_selectAjaxOption(self):
        url = "https://www.baidu.com/"
        #使用向下键选择
        self.driver.get(url)
        searchBox = self.driver.find_element_by_id('kw')
        searchBox.send_keys('世界杯')
        time.sleep(1)
        for i in range(3):
            searchBox.send_keys(Keys.DOWN)
            time.sleep(0.5)
        searchBox.send_keys(Keys.ENTER)
        time.sleep(2)

        #通过模糊匹配查询
        self.driver.get(url)
        try:
            searchBox2 = self.driver.find_element_by_id('kw')
            searchBox2.send_keys('世界杯')
            time.sleep(2)
            #模糊匹配内容会跟当前热词有所变化
            target_option = self.driver.find_element_by_xpath("//ul/li[contains(.,'时间')]")
            target_option.click()
            time.sleep(2)
        except WebDriverException:
            print(traceback.print_exc())
if __name__ == '__main__':
    unittest.main()

 

posted @ 2018-06-12 22:09  道生一_三生万物  阅读(191)  评论(0编辑  收藏  举报
returnTop $(function(){ $('#returnTop').click(function () { $('html,body').animate({ scrollTop: '0px' }, 800); returnfalse; }); });