如何在 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.com/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()
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP