Python 3.x 在Python 3 Selenium Webdriver中设置xpath上下文节点

Python 3.x 在Python 3 Selenium Webdriver中设置xpath上下文节点,python-3.x,selenium,selenium-webdriver,xpath,webdriver,Python 3.x,Selenium,Selenium Webdriver,Xpath,Webdriver,我想对某个节点重复执行xpath搜索。如何在python selenium webdriver中将此节点设置为上下文 from selenium import webdriver d=webdriver.Chrome() d.get("mysitehere") d.SET(context_node) <--what goes here? d.find_element_by_xpath("descendant::xxx[contains(text(), 'my text')]") d.fin

我想对某个节点重复执行xpath搜索。如何在python selenium webdriver中将此节点设置为上下文

from selenium import webdriver
d=webdriver.Chrome()
d.get("mysitehere")
d.SET(context_node) <--what goes here?
d.find_element_by_xpath("descendant::xxx[contains(text(), 'my text')]")
d.find_element_by_xpath("descendant::xxx[contains(text(), 'my other text')]")

您需要以下内容:

context = d.find_element_by_xpath("//*[@attr='value']")  # define context node

# Define descendants of context node
context.find_element_by_xpath("./descendant::xxx[contains(text(), 'my text')]")
context.find_element_by_xpath("./descendant::xxx[contains(text(), 'my other text')]")

请注意,您需要在XPath表达式的开头指定点以指向上下文节点

,这也是XPath新用户的注意事项,要搜索xxx必须相等的所有子体*