如何使用 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.tw/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.tw/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()

更新於: 2020 年 7 月 29 日

已閱讀 1 千餘次

開啟您的 職業生涯

完成課程獲得認證

開始
廣告