代码之家  ›  专栏  ›  技术社区  ›  ozo

使用Python和Selenium通过xpath正确选择web元素

  •  0
  • ozo  · 技术社区  · 7 年前

    <div>
        <div class = “class1” >
        <div class = “class2” >
        <div class = “class3” >
        <div style = “clear: both; ” >
    </div>
    <div>
        <div class = “class1” >
        <div class = “class2” >
        <div class = “class3” >
        <div style = “clear: both; ” >
    </div>    
    <div>
        <div class = “class1” >
        <div class = “class2” >
        <div class = “class3” >
        <div style = “clear: both; ” >
    </div>
    

    每个部分都有不同的信息。我想在类中搜索一个特定的词,如果这个词存在,那么我打印信息。在这之后,我有了问题。在我想得到本节第三节课的信息之后。例如,如果我在第一节中的类1有“this word”,那么我想在这节中获得类3的信息。

    cs1 = driver.find_elements_by_class_name("class1")
    for i in cs1:
        information = i.text
        if "this word" in information:
            print(information)
            infclass3 = i.find_element_by_xpath('//following-sibling::div[@class = "class3"]')
            print(infclass3.text)
    

    问题是:我用“this word”获得了1级信息,但这一节中关于3级的信息我没有。每次它都会打印第一节中的class3。例如,如果“this word”在第二节和第三节中,我会得到这样的结果:

    information of class1 - Section 2
    information of class3 - Section 1
    information of class1 - Section 3
    information of class3 - Section 1
    

    那么第1行和第3行中的信息是正确的。但在第2行和第4行中不是,1。因为是重复2。因为在第一节中不是“这个词”

    谢谢你的帮助。

    我希望你有一个愉快的一天:)

    2 回复  |  直到 7 年前
        1
  •  0
  •   Dalvenjia    7 年前

    您的代码的问题是您试图获取 class3 元素的上下文 class1 元素,这意味着它将只查找 当前分配给的元素 i ... 考虑到这一点 三班 您想要的元素是:

    infclass3 = i.find_element_by_xpath('../div[@class="class3"]')
    
        2
  •  0
  •   ozo    7 年前

    谢谢大家的帮助

    最后,我是这样理解的:

    infclass3 = i.find_element_by_xpath('following-sibling::*[2]')
    

    我得到了class1元素,然后用 'following-sibling::*[2]' 我找到了兄弟姐妹,并选择了一个位于位置2,对应于类3。