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

yipeiwu_com6年前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设计】。

相关文章

Python中函数的参数传递与可变长参数介绍

Python中函数的参数传递与可变长参数介绍

1.Python中也有像C++一样的默认缺省函数 复制代码 代码如下: def foo(text,num=0):     print text,num fo...

深入解析Python中的descriptor描述器的作用及用法

一般来说,一个描述器是一个有“绑定行为”的对象属性(object attribute),它的访问控制被描述器协议方法重写。这些方法是 __get__(), __set__(), 和 __...

Python中list列表的一些进阶使用方法介绍

判断一个 list 是否为空 传统的方式: if len(mylist): # Do something with my list else: # The list is e...

Python运行不显示DOS窗口的解决方法

方法1:pythonw xxx.py 方法2:将.py改成.pyw (这个其实就是使用脚本解析程序pythonw.exe) 跟 python.exe 比较起来,pythonw.exe 有...

python tkinter canvas 显示图片的示例

先来看一下该方法的说明 create_image(position, **options) [#] Draws an image on the canvas. position I...