当前位置:网站首页>Selenium element interaction
Selenium element interaction
2022-07-04 15:36:00 【Live up to your youth】
summary
selenium You can not only manipulate the browser , You can also manipulate elements in web pages .selenium The operations of elements in web pages mainly include Click on 、 Input content 、 Clear content 、 Form submission 、 Select... From the drop-down box Five parts .
Click on
element.click()
selenium Use click Function to implement element click operation ,click Function will simulate the mouse clicking in the center of the element , If the center of the element cannot be clicked for some reason , For example, elements are blocked , Then an element interrupt error is returned . Click element operation support HTML Any element in .
from selenium import webdriver
from selenium.webdriver.common.by import By
# open Chrome browser
driver = webdriver.Chrome()
# Navigate to Baidu homepage
driver.get("www.baidu.com")
# Get the home page search button
driver.find_element(By.ID, "su")
# Click on the element
element.click()
# Close the browser
dirver.quit()
Input content
element.send_keys()
selenium The function that implements the operation of input element content is send_keys function , Be careful ,selenium Not only can you enter text , And you can enter the name of the key as well as the text , It is equivalent to pressing the corresponding key in the keyboard . The input content operation only supports content editable elements , For example, input box <input type="text>.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# open Chrome browser
driver = webdriver.Chrome()
# Navigate to Baidu homepage
driver.get("www.baidu.com")
# Get input box
element = driver.find_element(By.ID, "kw")
# Enter webdriver Text and press Enter key
element.send_keys("webdriver" + Keys.ENTER)
# Close the browser
driver.close()
Clear content
element.clear()
Since there is input , Then there must be the operation of clearing content .selenium Delete element content , It's through clear Functionally implemented . The operation is consistent with the input content , The clear content operation also only supports content editable elements .
from selenium import webdriver
from selenium.webdriver.common.by import By
# open Chrome browser
driver = webdriver.Chrome()
# Navigate to Baidu homepage
driver.get("www.baidu.com")
# Get input box
element = driver.find_element(By.ID, "kw")
# Enter webdriver Text
element.send_keys("webdriver")
# Clear the input box text
element.clear()
# Close the browser
driver.close()
Form submission
stay Selenium 4 in , It is no longer implemented through separate endpoints and script execution methods . therefore , This method is not recommended , Instead, click the corresponding form submit button .
Select... From the drop-down box
select = Select(element)
For the operation of the drop-down box , When you get element After the object , be based on element Object to create one select object . then , call select Object to realize the operation of the drop-down box . This operation only supports HTML Drop down box element in , That is to say <select> Elements .
1、 choice
# Select an item in the drop-down box according to the index
select.select_by_index(index)
# according to option Of value Attribute select an item in the drop-down box
select.select_by_value(value)
# Select an item in the drop-down box according to the text of the options in the drop-down box
select.select_by_visible_text(text)
2、 deselect
# Deselect according to the index
select.deselect_by_index(index)
# according to value Property deselect
select.deselect_by_value(value)
# Deselect according to the text
select.deselect_by_visible_text(text)
# Deselect all selected options
select.deselect_all()
3、 Get all the selected options
select.all_selected_options
4、 Get the first of all the selected options
select.first_selected_option
5、 Get all items of the drop-down box (option Elements )
select.options
边栏推荐
- Introduction of text mining tools [easy to understand]
- AI has surpassed Dr. CS in question making?
- MySQL learning notes - data type (numeric type)
- Shell 编程基础
- Redis的4种缓存模式分享
- Scientific research cartoon | what else to do after connecting with the subjects?
- 宽度与对齐
- 這幾年爆火的智能物聯網(AIoT),到底前景如何?
- js平铺数据查找叶子节点
- 暑期复习,一定要避免踩这些坑!
猜你喜欢
MySQL学习笔记——数据类型(数值类型)
函数式接口,方法引用,Lambda实现的List集合排序小工具
暑期复习,一定要避免踩这些坑!
MySQL组合索引(多列索引)使用与优化案例详解
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Unity脚本常用API Day03
MP3是如何诞生的?
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
大神详解开源 BUFF 增益攻略丨直播
随机推荐
MySQL~MySQL给已有的数据表添加自增ID
文本挖掘工具的介绍[通俗易懂]
大神详解开源 BUFF 增益攻略丨直播
PXE network
go-zero微服务实战系列(九、极致优化秒杀性能)
Neuf tendances et priorités du DPI en 2022
Enter the width!
Preliminary exploration of flask: WSGI
Unity脚本介绍 Day01
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
宽度与对齐
C1 certification learning notes 3 -- Web Foundation
MySQL index optimization
Scientific research cartoon | what else to do after connecting with the subjects?
Usage of database functions "recommended collection"
Decimal, exponential
Redis shares four cache modes
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
Unity动画Animation Day05
MySQL学习笔记——数据类型(2)