python实现在Ajax方式产生的浮动框中,单击选择包含某个关键字的选项

有些被网页页面包含Ajax局部刷新机制,并且会产生显示多条数据的浮动框,需要单击选择浮动框中包含某个关键字的选项
以下是完整代码:
import unittest
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import traceback
class MyTestCase(unittest.TestCase):
def setUp(self):
#启动谷歌浏览器
self.driver=webdriver.Chrome(executable_path=’/python/driver/chromedriver’)
#通过模拟键盘下箭头进行悬浮框选项操作
‘’‘def test_AjaxDivOptionByKeys(self):
url=“http://www.sogou.com
self.driver.get(url)
searchBox=self.driver.find_element_by_id(“query”)
searchBox.send_keys(u"光荣之路")
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 test_AjaxDivOptionByKeys(self):
url = “http://www.sogou.com
try:
self.driver.get(url)
searchBox = self.driver.find_element_by_id(“query”)
searchBox.send_keys(u"光荣之路")
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())

if name == ‘main’:
unittest.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值