如何使用selenium单击网页上的按钮?
Selenium可以自动点击网页上出现的按钮。本文围绕如何使用Selenium在网页中单击任何按钮展开。为了做到这一点,我们必须采取两个主要步骤:
- 找到按钮。
- 点击按钮。
我们可以使用find_element_by_class_name() 、 find_element_by_name() 、 find_element_by_id()等方法找到网页上的按钮,然后我们可以使用click()方法单击它。
句法 :
# finding the button using ID
button = driver.find_element_by_id(ID)
# clicking on the button
button.click()
代码 :
Python3
import time
# importing webdriver from selenium
from selenium import webdriver
# Here Chrome will be used
driver = webdriver.Chrome()
# URL of website
url = "https://www.geeksforgeeks.org/"
# Opening the website
driver.get(url)
# getting the button by class name
button = driver.find_element_by_class_name("slide-out-btn")
# clicking on the button
button.click()
这将单击按钮并显示一个弹出窗口。
输出 -