如何使用 Python 中的 Selenium 选中页面中的复选框?
我们可以在 Selenium 中使用 click() 方法选中页面中的复选框。首先我们需要使用任何定位器(如css、xpath、id、类等)唯一标识复选框。
接下来,我们必须使用 findElement() 方法找到元素,最后执行点击操作。如果页面上找不到匹配的元素,将会抛出异常。
语法
driver.find_element_by_xpath("//input[@name='check-box']")举例
复选框选择代码实现。
from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/selenium/selenium_automation_practice.htm")
#to refresh the browser
driver.refresh()
# identifying the checkbox with xpath, then click
driver.find_element_by_xpath("//input[@value='Automation Tester']")
.click()
#to close the browser
driver.close()
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP