【问题标题】:Setting the xpath context node in Python 3 Selenium Webdriver在 Python 3 Selenium Webdriver 中设置 xpath 上下文节点
【发布时间】:2018-07-04 10:40:57
【问题描述】:

我想对某个节点执行重复的 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')]")

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver xpath webdriver


    【解决方案1】:

    您需要以下内容:

    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 必须等于 * 的所有后代
    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2019-02-22
    • 2012-09-27
    相关资源
    最近更新 更多