首页 > 解决方案 > 搜索

  • 通过子文本列出
  • 问题描述

    所以我目前正在使用python从excel表中导入数据,然后获取该信息并使用它来填写网页上的表格。

    我遇到的问题是选择下拉菜单的配置文件。

    我一直在使用 Selenium 库,我实际上可以使用 find_element_by_xpath 假设来选择元素,但这是假设我知道数据值,数据值是为每个添加的新配置文件自动生成的,所以我不能将其用作可靠的手段。

    Profile = Browser.find_element_by_xpath("/html/something/something/.....")
    Profile.click()
    time.sleep(0.75)  #allowing time for link to be clickable
    The_Guy = Browser.find_element_by-xpath("/html/something/something/...")
    The_Guy.click()
    

    这仅适用于已知路径我想做这样的事情

    Profile = Browser.find_element_by_xpath("/html/something/something/.....")
    Profile.click()
    time.sleep(0.75)  #allowing time for link to be clickable
    The_Guy = Browser.find_element_by_id("Caption.A")
    The_Guy.click()
    

    HTML 示例

    
        <ul class ="list">
        <li class = "option" data-value= XXXXX-XXXXX-XXXXX-XX-XXX>
           ::marker
            Thor
        </li>
        <li class = "option" data-value= XXXXX-XXXXX-XXXXX-XX-XXX>
           ::marker
            IronMan
        </li>
        <li class = "option" data-value= XXXXX-XXXXX-XXXXX-XX-XXX>
           ::marker
            Caption.A
        </li>
        ....
        </ul>
    

    我希望能够通过名称搜索(如 Caption.A),然后退后一步选择父 Li。提前致谢

    标签: pythonhtml

    解决方案


    尝试使用以下 xpath 找到包含所需文本的 li,然后单击它。示例代码:

    driver.find_element(By.xpath("//li[contains(text(), 'Caption.A')]")).click()
    

    希望能帮助到你 :)


    推荐阅读