python - 使用 selenium python 从下拉选项中选择一个值

标签 python selenium

我想从下拉选项中选择一个值。 html如下:

<span id="searchTypeFormElementsStd">

    <label for="numReturnSelect"></label>
    <select id="numReturnSelect" name="numReturnSelect">
        <option value="200"></option>
        <option value="250"></option>
        <option value="500"></option>
        <option selected="" value="200"></option>
        <option value="800"></option>
        <option value="15000"></option>
        <option value="85000"></option>
    </select>

</span

我尝试如下:

find_element_by_xpath("//select[@name='numReturnSelect']/option[text()='15000']").click()

它有什么问题?请帮帮我!

最佳答案

Adrian Ratnapala 是对的,我也会选择 id 而不是 name,所以您可以尝试以下方法:

find_element_by_xpath("//select[@id='numReturnSelect']/option[@value='15000']").click()

find_element_by_css_selector("select#numReturnSelect > option[value='15000']").click()

您可以使用 select_by_value(value) :

Select(driver.find_element_by_css_selector("select#numReturnSelect")).select_by_value(15000).click()

点击这里了解更多关于 Select 的信息.

关于python - 使用 selenium python 从下拉选项中选择一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22524621/

相关文章:

python - 不了解页面结构的网页抓取

python - 属性错误: 'WindowsPath' object has no attribute 'endswith'

python - Scrapy - 动态等待页面加载 - selenium + scrapy

python - 如何获取 python selenium 中的所有数据?

python - cv2.imwrite()未将时间戳写入帧

python - 覆盖更新和 to_representation 方法在 POST 请求时抛出 AttributeError

python - django admin 与 GeoModelAdmin 内联

python - 尝试在win7上设置selenium/phantomjs的路径

java - 全屏操作在 selenium webdriver 3.x 中不起作用

selenium - 使用 Selenium 和 Protractor/Jasmine 时出现 Chrome 自动化扩展错误