如何使用 Python 中的 Selenium 工具包中的 click() 方法?
在处理应用程序以及导航到不同页面或某个页面的不同部分时,我们需要点击页面上各种 UI 元素,比如链接或按钮。所有这些都是借助 click() 方法完成的。
因此,click() 方法通常用于处理按钮和链接等元素。
语法
driver.find_element_by_xpath("//button[id ='value']").click()示例
使用 click() 方法点击链接的编码实现。
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/about/about_careers.htm")
#to refresh the browser
driver.refresh()
# identifying the link then using click() method
driver.find_element_by_link_text("Company").click()
#to close the browser
driver.close()使用 click() 方法点击按钮的编码实现。
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/about/about_careers.htm")
#to refresh the browser
driver.refresh()
# identifying the button then using click() method
driver.find_element_by_xpath("//button[contains(@class,'gsc-search')]") .click()
#to close the browser
driver.close()
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP