您将如何使用 Python 中的 xpath 在 Selenium 中从子节点导航到父节点?
利用 xpath,我们可以在 DOM 中从其子节点识别父节点。在某些情况下,html 中的父节点具有动态属性,而子节点具有一些可用于识别的唯一静态属性。
通过联合使用相对 xpath 和父 xpath 轴。方法可达成此目的。
语法
driver. find_element_by_xpath("//input[@id='job']/parent::div")示例
子节点到父节点遍历的代码实现。
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/tutor_connect/index.php")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of xpath parent
atb=driver.find_element_by_xpath ("//input[@id='txtSearchText']/parent::div")
#prints the class attribute value in console
val = atb.get_attribute("class")
print(val)
driver.close()
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP