【问题标题】:How to click option in dropdown menu which does not have "select" tag using Selenium Python?如何使用 Selenium Python 在没有“选择”标签的下拉菜单中单击选项?
【发布时间】:2018-02-08 07:51:28
【问题描述】:

我关心的下拉列表的html代码 ]

这是我正在谈论的“Helfulness”下拉菜单的链接。 https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en

访问 selenium 页面后,我想自动执行选择第一个下拉列表并选择“最新”选项的过程。问题是下拉菜单没有选择标签。有人可以帮忙吗?

【问题讨论】:

  • 我在上面的链接中没有看到任何下拉菜单

标签: python html selenium drop-down-menu


【解决方案1】:

尝试使用 Selenium IDE 获取下拉菜单的名称、id 或 xpath。

【讨论】:

    【解决方案2】:

    当我们想从没有选择标签的下拉列表中选择值时,我们已经构建了选择它的逻辑。第一步是单击下拉列表,然后为下拉列表的值找到一个通用定位器.定位器应该完全依赖于下拉列表的值,这样只需添加所需的值,我们就可以得到唯一的定位器,如下所示

    public void selectValueFromDropDown(WebDriver driver,String value) throws InterruptedException{
            //click on the dropdown
            driver.findElement(By.xpath("//button[@class='dropdown-menu'][1]")).click();
            Thread.sleep(1000);
            // select value from dropdown
            driver.findElement(By.xpath("//div[@class='dropdown-menu-children']//li//button[text()='"+value+"']")).click();
        }
    

    希望这会对你有所帮助。

    【讨论】:

    • 对您有帮助吗?
    【解决方案3】:

    1。硒

    您可以使用 XPath 通过其文本找到该按钮:

    button = driver.find_element_by_xpath("//button[./text() = 'Helpfulness']")
    button.click()
    

    2。使用 Capybara(使用 Selenium)

    更普遍(并且可靠),您可以使用capybara-py 来点击各种按钮(不仅仅是<button> 元素):

    page.click_button("Helpfulness")
    

    如果按钮还不能交互,它也会正确地转义你的搜索字符串并重试。

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2018-02-17
      • 1970-01-01
      • 2023-04-09
      • 2016-07-28
      • 2022-08-13
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多