【问题标题】:How can I select an element from this drop down menu in Python/Selenium?如何从 Python/Selenium 的下拉菜单中选择一个元素?
【发布时间】:2013-06-07 01:15:49
【问题描述】:

我查看了一些选择下拉元素的解决方案,但没有一个适合我。

这是下拉菜单的 html。

<div class="goog-inline-block goog-flat-menu-button" role="button" style="-moz-user-select: none;" tabindex="3" aria-haspopup="true">
    <div class="goog-inline-block goog-flat-menu-button-caption">Resolved</div>
    <div class="goog-inline-block goog-flat-menu-button-dropdown">&nbsp;</div>
</div>

我尝试通过 xpath 和链接文本找到下拉列表,但没有成功。

编辑:这是我正在使用的代码

import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import re
with contextlib.closing(webdriver.Firefox()) as driver:
    driver.get("https://websitename.com/#ticket/123456")
    wait = ui.WebDriverWait(driver, 30)
    wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='goog-inline-block goog-flat-menu-button"))
    driver.find_element_by_xpath("//div[@class='goog-inline-block goog-flat-menu-button").click()
    driver.find_element_by_xpath("//div[@class='goog-inline-block goog-flat-menu-button").send_keys("R")
    wait6 = ui.WebDriverWait(driver, 30)

代码应该将选定的下拉列表元素从已分配更改为已解决。

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    先展开下拉菜单(xpath=//div[@class='goog-inline-block goog-flat-menu-button')然后点击选项将其选中。

    这两个操作都可以通过在 Selenium IDE 中单击来完成。
    如果单击不起作用,请尝试 clickAt 定位器。

    【讨论】:

    • 我试过了,但它无法识别 xpath。我用我尝试使用的代码编辑了我的原始问题。
    【解决方案2】:

    你尝试了什么 xpath?

    我能看到的最简单的是:

    driver.find_element_by_xpath("//div[contains(@class, 'goog-flat-menu-button-dropdown')]")
    

    但是,我对您关于“选择”的请求感到有些困惑,因为这不是 select 元素,所以很抱歉,我不确定我是否能提供帮助。

    在 CSS 选择器的帮助下尝试下面给出的方法

    driver.find_element_by_cssselector(".goog-flat-menu-button > .goog-flat-menu-button-dropdown").click();
    

    我确信上面的 CSS 选择器会起作用。

    【讨论】:

    • 我应该更清楚。我想更改下拉列表中的选定元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2020-05-14
    • 1970-01-01
    • 2021-12-30
    • 2019-02-25
    • 2016-04-13
    相关资源
    最近更新 更多