【问题标题】:exact string match for selenium dropdownselenium 下拉列表的精确字符串匹配
【发布时间】:2022-09-26 16:02:47
【问题描述】:

我正在尝试将字符串与网站上的下拉菜单完全匹配

    # Check if a specific position is chosen or if it is left default / empty
    if str_sv_pos == \"position\" or str_sv_pos == \"\":
        pass
    else:
        try:
            # press on the drop down container
            pos_dropdown_menu = init_webdriver.driver.find_element_by_xpath(
                \"/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[4]/div\"
            )
            pos_dropdown_menu.click()
            
            # define variable \"my_pos_string\" taken out of a input field in my GUI
            my_pos_string = str(str_sv_pos)

            # choose the correct position from the container list
            find_pos = init_webdriver.wait.until(
                EC.element_to_be_clickable(
                    (
                        By.XPATH,
                        (
                            \"//li[@class=\'with-icon\' and contains(text(),\'\"
                            + my_pos_string
                            + \"\')]\"
                        ),
                    )
                )
            )
            find_pos.click()
        except:
            pass

这适用于大多数职位。

但问题是,容器内有一个“LW”和一个“LWB”。并且 \"LWB\" 出现在 \"LW\" 之前,所以它选择了 \"LWB\"。

所以我想完全匹配我在 GUI 中的输入以避免这种情况。

在我的示例中如何做到这一点?

标签: python string selenium tkinter match


【解决方案1】:

不要使用contains() XPath 函数,而是使用text() XPath 函数。

This answer to a very similar question on SO 应该告诉您解决问题所需的一切。

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    相关资源
    最近更新 更多