Splinter入门(八) Interacting with elements 元素交互(三)
Type() 方法
前面说到,可以使用fill()
填充文本框,但是无法检测JavaScript
事件,而Type()
可以实现。
browser.type('type', 'typing text')
用于给name
为type
的文本框填充typing text
文本。这和fill()
是一样的。如果给type传递一个参数slowly=True
,则可以监听每个字符输入,例如:
for key in browser.type('type', 'typing slowly', slowly=True):
pass # make some assertion here with the key object :)
此外还可以:
browser.find_by_name('name').type('Steve Jobs', slowly=True)
browser.find_by_css('.city').fill('San Francisco')
元素是否可见(visible or invisible)
browser.find_by_css('h1').first.visible # 可见返回ture
browser.find_by_css('h1').first.invisible # 不可见返回true
元素是否含有某class属性
browser.find_by_css('.content').first.has_class('content')
ElementList对象
通过Find_*
函数返回的是一个list(ElementList对象),里面可能有多个html的元素,如果需要对第一个元素操作,可以类似:
browser.find_by_css('a.my-website').first.click()
Splinter
中可以允许任何Element
的方法用在ElementList
上,这样默认会将使用ElementList
的第一个Element
。即以下两行是等价的:
browser.find_by_css('a.banner').first.visible
browser.find_by_css('a.banner').visible
示例
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表单应用</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<style>
.h1{
color: brown;
}
.content{
color: burlywood;
}
</style>
<script>
function myFunction(){
// alert('输入框更新啦~',1)
}
</script>
<body>
<h1 class="h1">表单应用</h1>
<h1 class="h1" hidden="True">隐藏的表单应用</h1>
<div>
<form>
name:<input id = "nameId1" type="text" name="firstname" onchange="myFunction()"><br>
name:<input id = "nameId2" type="text" name="secondname" class="content"><br>
Sex:<input id="sexId" type="radio" name="sex" value="male">male <input id="sexId" type="radio" name="sex" value="female">female<br>
Vehicle:<input id="vehicleId" type="checkbox" name="vehicle" value="Bike">I have a bike<br>
file: <input type="file" id="inName" name = 'file' mutiple="multiple">
</form>
</div>
</body>
</html>
python脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : cuntou0906
# @File : 03.py
# @Time : 2021/7/12 22:38
from splinter import Browser
from time import sleep
browser = Browser('chrome') # 创建浏览器实例
browser.visit('file:///E:/My_code/Python_code/PythonWebCode/SplinterCode/05_Type/html/index2.html') # 访问baidu
browser.type('firstname', 'typing text')
sleep(1)
print("type::")
for key in browser.type('firstname', 'typing slowly', slowly=True):
print(key)
pass # make some assertion here with the key object :)
sleep(1)
browser.find_by_name('secondname').fill('San Francisco')
sleep(1)
print("visible测试::")
print("第一个h1是否可见:",browser.find_by_css('h1').first.visible)
print("第2个h1是否可见:",browser.find_by_css('h1')[1].visible)
sleep(1)
print("has_class测试::")
print(browser.find_by_css('.content').first.has_class('content'))
print(browser.find_by_css('.content').first.has_class('h1'))
print("获取ElementsList里的第一个元素进行操作:")
print(browser.find_by_css('.h1').first.visible)
print("直接对ElementsList进行操作:")
print(browser.find_by_css('.h1').visible)
browser.quit() # 关闭浏览器
运行结果
type::
t
y
p
i
n
g
s
l
o
w
l
y
visible测试::
第一个h1是否可见: True
第2个h1是否可见: False
has_class测试::
True
False
获取ElementsList里的第一个元素进行操作:
True
直接对ElementsList进行操作:
True