【问题标题】:How to select an <option> within a <select> tag using Selenium in Python 3?如何在 Python 3 中使用 Selenium 在 <select> 标记中选择 <option>?
【发布时间】:2020-09-28 07:31:11
【问题描述】:

这里是 Python 新手,所以在此先感谢您,如果我说一些非常明显的话,我们深表歉意。

我想点击的选项非常嵌套(不知道它是否有区别,可能是因为我对这个主题很无知)。嵌套:

<frameset <frame <html <body <form <table <tbody <tr <td <p <select <option

选择有 name='base' 和 size=15,就是这样。 该选项的 value='B101' 和 Text='Cam'

这是我所拥有的:

table = driver.find_element_by_name('base')
         for option in table.find_elements_by_tag_name('option'):
             if option.text == 'Cam':
                 option.click()
                 break`

但在第一行中断,出现此错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="base"]"}

也尝试使用 xpath,但我得到了这个:/html/body/form/table/tbody/tr[2]/td[2]/input,(而不是我习惯的 //*[@id=...),它也找不到它。不是按类“选择”。

还尝试通过 xpath、名称、类直接进入选项,但没有任何效果。 我似乎无法单击该选项。不知道这是否有所不同,但我在 Mac 上并使用 ChromeDriverManager。 单击选项后,我必须单击“选择”才能转到下一页。

编辑: 谢谢Shreyas!请原谅我的无知,我这样做了,它不会带来错误,但我也没有看到选择的选项。单击该选项后,我必须单击“选择”按钮(标记“输入”),这在 Selenium 中也找不到。这是 HTML 以防万一:

<frameset rows="40,*,25" frameborder="0" framespacing="0">
    <frame name="Results" src="/documents/bases.jsp" marginwidth="3" marginheight="3">
        #document
            <html><head><title>Base</title>
<meta http-equiv="expires" content="Wed, 26 Feb 1997">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" marginwidth="3" marginheight="3">
<form method="POST" action="/documents/bases.jsp">
<input type="hidden" name="tdb" value="B">
<table align="center" border="0">
    <tbody><tr>
        <td valign="top"><img src="/images/bases.jpg" width="30" height="28"></td>
        <td>
            <p align="center"><font color="#804040" size="4" face="Verdana"><b>
            Available Base
            </b></font></p>
            <p align="center">
            <select name="base" size="15" multiple="">
<option value="B096">Not_this_option</option>
<option value="B101">This_option</option>
<option value="B109">Not_this_option</option>
<option value="B110">Not_this_option</option>
<option value="B151">Not_this_option</option>
<option value="B158">Not_this_option</option>
<option value="B170">Not_this_option</option>
<option value="B219">Not_this_option</option>
<option value="B221">Not_this_option</option>
<option value="B222">Not_this_option</option>
<option value="B223">Not_this_option</option>
<option value="B261">Not_this_option</option>
<option value="B300">Not_this_option</option>
<option value="B351">Not_this_option</option>
<option value="B352">Not_this_option</option>
<option value="B502">Not_this_option</option>
<option value="B511">Not_this_option</option>
<option value="B649">Not_this_option</option>
<option value="B650">Not_this_option</option>
<option value="B660">Not_this_option</option>
<option value="B704">Not_this_option</option>
            </select>
            </p>
        </td>
        <!--
        <td align='center' valign='top'>
            <table border='1'>
                <tr>
                    <td align='center'>
                        <font size='2'><b>What kind<br>
                        Of base do you wanna see?</b></font><p>
                        <a href='bases.jsp?tdb=B' target='Results'>Kind1/a><br>
                        <a href='bases.jsp?tdb=F' target='Results'>Kind2</a></p>
                    </td>
                </tr>
            </table>
        </td>
        -->
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td align="center">
            <input type="submit" name="Select" value="Select">
        </td>
        <td>&nbsp;</td>
    </tr>
</tbody></table>
</form>
</body></html>

【问题讨论】:

    标签: python html selenium webdriver frame


    【解决方案1】:
    1. 首先找到要选择的选项元素的值。
    2. 然后你可以运行javascript来改变select元素的值。
    value = "B101"
    driver.execute_script('var elements = document.getElementsByTagName("select");
                          for (element of elements) {
                            element.value = ' + value + '
                          }') 
    

    document.getElementByTagNames('select') 获取所有带有 select 标签的元素并返回所有这些元素的数组

    然后,我们循环遍历元素列表中的每个元素,并将 select 标签的值更改为您想要的值。

    【讨论】:

      【解决方案2】:

      找到了! 由于 HTML 有框架,首先你必须切换到正确的框架:

      driver.switch_to.frame(driver.find_element_by_name('Results'))
      Option = driver.find_element_by_xpath("//*[contains(text(), 'This_option')]")
      Option.click()
      Select = driver.find_element_by_name('Select')
      Select.click()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 2012-05-12
        相关资源
        最近更新 更多