本篇文章讲的是select属性的下拉框。
遇到下拉框的时候,我们不能用click或send_kay。那使用什么呢?
答案是select方法,但是网页元素中没有select这个方法。怎么办呢?
答案是去创建一个下拉框的类,让这个类继承网页元素中的所有方法,并写出自己的方法。怎么用呢?
先找出页面元素,转换成下拉框的类型,然后直接调用选择的方法。
下面看一个例子:
1、选择城市,河北省
代码写法:
# 下拉框省份的定位
sheng=driver.find_element_by_id('add-new-area-select')
Select(sheng).select_by_value('110000')
2、选择市,石家庄

代码写法
本篇文章讲的是select属性的下拉框。遇到下拉框的时候,我们不能用click或send_kay。那使用什么呢?答案是select方法,但是网页元素中没有select这个方法。怎么办呢?答案是去创建一个下拉框的类,让这个类继承网页元素中的所有方法,并写出自己的方法。怎么用呢?先找出页面元素,转换成下拉框的类型,然后直接调用选择的方法。下面看一个例子:1、选择城市,河北省代码写法:...
这里可以使用XPAN定位到下拉框,使用driver.find_element(By.XPATH,“路径”).click()进行点击
2、定位到下拉框里的筛选项元素,每次滚动都会变化
这里的内容要用XPAN定位到//div[@class=“rc-virtual-list-holder-inner”]
然后使用 鼠标滚动的方式,去获取所有的元素名称
def mouse_move(self,loc,page_nam
from tkinter import *
from ComBoPicker import Combopicker# 导入自定义下拉多选框
if __name__ == "__main__":
root = Tk()
root.geometry("200x200")
main =Frame(root)
select_by_index(index) 通过索引定位
select_by_value(value) 通过value值定位
select_by_visible_text(text) 通过文本内容定位
2.html 代码如下,大家可以复制一下内容然后保存为select.html格式
对于select类型的下拉框,可以调用select类的select方法去定位:
定位到要选择的下拉框 element = driver.find_element(...)
把找到的页面元素,转换成下拉框的类型Select:select = Select(element)
调用Select类中的select方法:
通过Value值:select.select_by_value(选项的value属性的值)
通过index值:select.select_by_index(第几个选项)
通过文本:s
在 Python Selenium 中,可以使用 Select 类来操作下拉框。首先需要导入 Select 类,然后使用 WebDriver 定位到下拉框元素,将其传入 Select 类的构造函数中,就可以对下拉框进行操作了。
例如,以下代码可以用来选择下拉框中的第二项:
```python
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id("example"))
select.select_by_index(1)
你也可以用 select_by_value() 或 select_by_visible_text() 来选择下拉框中的选项。
```python
select.select_by_value("value")
select.select_by_visible_text("text")
记得使用完之后要关闭浏览器窗口。