python3 selenium自动化 下拉框定位的例子

yipeiwu_com7年前Python基础

我们在做web UI自动化时,经常会碰到下拉框,如下图:

所上图,下拉框的源代码如下:

<html1>
 <head></head>
 <body>
  <select id="fruit" name="水果" style="width:100px;">
  <option value ="0">苹果</option>
  <option value ="1">香蕉</option>
  <option value="2">菠萝</option>
  <option value="3">梨子</option>
 </body>
</select>

假如我们要选择‘菠萝',我们将怎么实现呢?

首先我们要定位水果框,再定位水果下面的元素,如下图所示:

具体代码如下:

from selenium import webdriver
from selenium.webdriver.support.select import Select #首先必须要导入select包才能定位
from time import sleep

dr = webdriver.Chrome()
dr.get(r'D:\下拉框.html')

#先定位到水果框,用变量selectfruit
selectFruit = dr.find_element_by_id('fruit')

#再定位到具体的元素,菠萝
Select(selectFruit).select_by_visible_text('菠萝')

定位完毕,收工。

以上这篇python3 selenium自动化 下拉框定位的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3.6中@property装饰器的使用方法示例

本文实例讲述了python3.6中@property装饰器的使用方法。分享给大家供大家参考,具体如下: 1、@property装饰器的使用场景简单记录如下: 负责把一个方法变成属性...

Python WEB应用部署的实现方法

Python WEB应用部署的实现方法

本文介绍了Python WEB应用部署的实现方法,分享给大家,具体如下: 使用Apache模块mod_wsgi运行Python WSGI应用 Flask应用是基于WSGI规范的,所以...

python scipy求解非线性方程的方法(fsolve/root)

python scipy求解非线性方程的方法(fsolve/root)

使用scipy.optimize模块的root和fsolve函数进行数值求解线性及非线性方程,下面直接贴上代码,代码很简单 from scipy.integrate import o...

python实现类之间的方法互相调用

all.py from son import * class ALL(): def __init__(self): self.mSon = SON(self) def get...

Python实现的微信红包提醒功能示例

本文实例讲述了Python实现的微信红包提醒功能。分享给大家供大家参考,具体如下: #coding=utf-8 import itchat from itchat.content i...