selenium的关于选择框的定位

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.implicitly_wait(10)
mouse = driver.find_element_by_link_text(u'设置')
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text(u'搜索设置').click()
time.sleep(1)
'''第一种方式, 直接用xpath定位到要选中的项
driver.find_element_by_xpath('//*[@id="nr"]/option[3]').click()
'''

'''第二种方式,使用select选中器
sel = driver.find_element_by_xpath('//*[@id="nr"]') #先定位到选择框
time.sleep(1)
Select(sel).select_by_index(2) #根据选择框的索引去选中
Select(sel).select_by_visible_text('每页显示50条') #根据text值来选中
Select(sel).select_by_value('50') #根据walue值来选中
'''

备注:若是非正规的选择器,则直接使用第一种方式,或者是按照人工操作顺序,先点选择框,后点需要选中的元素

转载于:https://www.cnblogs.com/yueyanru/p/9393661.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值