当前位置:网站首页>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
边栏推荐
- LeetCode 58. 最后一个单词的长度
- 中国主要城市人均存款出炉,你达标了吗?
- PXE网络
- Unity动画Animation Day05
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- 一篇文章搞懂Go语言中的Context
- Usage of database functions "recommended collection"
- Functional interface, method reference, list collection sorting gadget implemented by lambda
- . Net applications consider x64 generation
- Redis shares four cache modes
猜你喜欢
Huawei cloud database DDS products are deeply enabled
Redis 發布和訂閱
Redis的4种缓存模式分享
Unity animation day05
2022年九大CIO趨勢和優先事項
.Net 应用考虑x64生成
In today's highly integrated chips, most of them are CMOS devices
Redis哨兵模式实现一主二从三哨兵
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
随机推荐
Unity script API - component component
Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
Unity脚本生命周期 Day02
Redis publish and subscribe
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
selenium 元素交互
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Functional interface, method reference, list collection sorting gadget implemented by lambda
Unity prefab day04
Logstash ~ detailed explanation of logstash configuration (logstash.yml)
Data Lake Governance: advantages, challenges and entry
宽度与对齐
Unity script lifecycle day02
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
输入宽度!
Lombok使用引发的血案
Unity预制件Prefab Day04
MYSQL索引优化
On the implementation plan of MySQL explain
MySQL - MySQL adds self incrementing IDs to existing data tables