def setUp(self):
# 启动IE浏览器
self.driver = webdriver.Ie(executable_path="g:\\IEDriverServer")
def test_printSelectText(self):
url = "http://127.0.0.1/test_select.html"
# 访问自定义的html网页
self.driver.get(url)
# 使用name属性找到页面上name属性为“fruit”的下拉列表元素
select = self.driver.find_element_by_name("fruit")
all_options = select.find_elements_by_tag_name("option")
for option in all_options:
print("选项显示的文本:", option.text)
print("选项值为:", option.get_attribute("value"))
option.click()
time.sleep(1)
def tearDown(self):
# 退出IE浏览器
self.driver.quit()
if __name__ == '__main__':
unittest.main()
#点击下拉框选项option.click()import unittestimport timefrom selenium import webdriverclass VisitSogouByIE(unittest.TestCase): def setUp(self): # 启动IE浏览器 self.driver = webdriver.Ie(e...
使用python爬虫的
selenium
操作
网页的下
拉
框。
以该网站为例:https://www.17sucai.com/pins/demo-show?id=5926
该网页下存在多个可供测试的下
拉
框。
基本脚手架代码:
from
selenium
.webdriver.support.ui import Select
from
selenium
import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www
1、在Web网页中经常会遇到下
拉
框(下
拉
列表):比如说分页、每页显示的条数、语言的切换等功能,很多时候经常都是以下
拉
框的形式展现
2、下
拉
列表是网页中一种最节省页面空间的选择方式,默认状态下只显示一个
选项
,只有单
击
下
拉
按钮后才能看到全部的
选项
进而进行选择
3、在实际Web网页开发中下
拉
框的实现方式有很多:
⑴<select>和<option>实现下
拉
框
⑵<ul>和<li>实现下
拉
框
4、对于不同方式实现的下
拉
框...
首先是浏览器驱动选择,必须选32位的IEDriverServer.exe,64位的问题是填入用户名密码时会非常卡顿。
然后是虽然
selenium
找到了元素。但是直接调用会毫无反应的问题:
menu = driver.find_element_by_link_text("[下一页]").click()
解决方案三种:
第一,利用
selenium
找到元素,传入js,然后直接调用js即可
menu = driver.find_element_
很多时候我们在做
自动化
时,需要处理下
拉
菜单事件。
举个例子吧,在好搜的搜索结果页右上角,有这样一个下
拉
菜单:鼠标移动上去之后,会显示出baidu、bing、google的引擎切换
选项
。
但是当下
拉
菜单不显示的时候,我们用driver.find_element_by_class_name方法去获取element,会出异常:
selenium
.common.exceptions.Ele
selenium
操作
页面下
拉
框,一般来说分为两种情况,一种是标准的select标签下
拉
框;另一种是非select类下
拉
框,比如ul-li标签下
拉
框。针对select标签的下
拉
框,
selenium
提供了一个select类;针对非select标签实现的下
拉
框,可以通过模拟鼠标点
击
的方式实现
操作
。
**1、select标签下
拉
框**
如下图所示,下
拉
框为select标签,此时可以使用select类提供的三种方法定位到下
拉
框中的元素。
# 找到需要拖拽的元素
source_element = driver.find_element_by_xpath("//div[@id='source']")
target_element = driver.find_element_by_xpath("//div[@id='target']")
# 对元素执行拖拽
操作
ActionChains(driver).drag_and_drop(source_element, target_element).perform()
以上是常用的鼠标
操作
示例,你可以根据需要进行组合和扩展。
出现selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath的解决方案
22591
出现selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath的解决方案
出现UnboundLocalError: local variable 'a' referenced before assignment异常的情况与解决方法
StefniaXU:
出现IndentationError: unexpected indent或者TabError: inconsistent use of tabs and spaces异常的情况及解决方法
HTTP协议之Cookie和Session
Python文件操作之读取文件中的tuple、list、dict