Splinter入门(八) Interacting with elements 元素交互(三)

Splinter入门(八) Interacting with elements 元素交互(三)

Type() 方法

 前面说到,可以使用fill()填充文本框,但是无法检测JavaScript事件,而Type()可以实现。

browser.type('type', 'typing text')用于给nametype的文本框填充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是否可见: True2个h1是否可见: False
has_class测试::
True
False
获取ElementsList里的第一个元素进行操作:
True
直接对ElementsList进行操作:
True
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cuntou0906

玛莎拉蒂是我的目标!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值