如何在 python 中用 Selenium 使用 xpath 中的 text()?
我們可以藉助頁面上的可見文字建立自定義 xpath。這是透過 xpath 中的 text() 方法實現的。
text() 找到頁面上完全匹配文字的物件。
語法
driver.find_element_by_xpath("//input[text()='Selenium']")
它將在頁面上搜索可見文字為“Selenium”的元素。
示例
使用 text() 的程式碼實現。
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/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of text() in xpath driver.find_element_by_xpath("//*[text()='GATE Exams']").click() #to close the browser driver.close()
廣告