使用Selenium Python从下拉选项中选择一个值

发布于 2021-02-01 11:39:40

我想从下拉选项中选择一个值。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()

怎么了 请帮我!

关注者
0
被浏览
101
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

    阿德里安Ratnapala是正确的,也是我会选择idname,所以你可以尝试以下方法:

    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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看