在Ajax方式产生的浮动框中,点击选项包含某个关键字的选项

 

#!usr/bin/env python  
#-*- coding:utf-8 -*-  
""" 
@author:   sleeping_cat
@Contact : zwy24zwy@163.com 
""" 

#在Ajax方式产生的浮动框中,点击选项包含某个关键字的选项
#通过模拟键盘下箭头进行选择悬浮框选项

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time

class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_AjaxDivOptionByKeys(self):
        url = 'http://www.sogou.com/'
        self.driver.get(url)
        searchBox = self.driver.find_element_by_id('query')
        searchBox.send_keys('光荣之路')
        time.sleep(2)
        for i in range(3):#选择悬浮窗中第几个联想关键词选项就循环几次
            searchBox.send_keys(Keys.DOWN)#模拟键盘向下箭头
            time.sleep(0.5)
        searchBox.send_keys(Keys.ENTER)
        time.sleep(3)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

 

#!usr/bin/env python  
#-*- coding:utf-8 -*-  
""" 
@author:   sleeping_cat
@Contact : zwy24zwy@163.com 
"""
#在Ajax方式产生的浮动框中,点击选项包含某个关键字的选项
#通过匹配模糊内容选择悬浮框中选项

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import traceback
import time
import unittest

class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_AjaxDivOptionByKeys(self):
        url = 'http://www.sogou.com/'
        self.driver.get(url)
        try:
            searchBox = self.driver.find_element_by_id('query')
            searchBox.send_keys('光荣之路')
            time.sleep(2)
            suggetion_option = self.driver.find_element_by_xpath('//ul/li[contains(.,"免费观看")]')
            suggetion_option.click()
            time.sleep(2)
        except NoSuchElementException as e:
            print(traceback.print_exc())

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
posted @ 2017-12-26 15:00  sleeping_cat  阅读(281)  评论(0编辑  收藏  举报