简体   繁体   English

如何选择一个<option>在一个<select>在 Python 3 中使用 Selenium 标记?

[英]How to select an <option> within a <select> tag using Selenium in Python 3?

new to Python here so thanks in advance and sorry if I say something really basically obvious.这里是 Python 的新手,所以提前致谢,如果我说的东西基本上很明显,我很抱歉。

The option I want to click on is quite nested (don't know if it makes a difference, it might since I'm pretty ignorant in the subject).我想点击的选项是完全嵌套的(不知道它是否有区别,可能是因为我对这个主题非常无知)。 The nesting goes:嵌套进行:

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

The select has name='base' and size=15, thats it.选择有 name='base' 和 size=15,就是这样。 The option has value='B101' and Text='Cam'该选项具有 value='B101' 和 Text='Cam'

Here is what I have:这是我所拥有的:

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

But is breaks at the first line, with this error但是在第一行中断,出现这个错误

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

Also tried using the xpath but i get this: /html/body/form/table/tbody/tr[2]/td[2]/input , (instead of what I am used to something like //*[@id=... ) and it doesn't find it either.也尝试使用 xpath 但我得到了这个: /html/body/form/table/tbody/tr[2]/td[2]/input ,(而不是我习惯的类似//*[@id=... ),它也没有找到。 Not by class 'select'.不是按类“选择”。

Also tried going directly to the option by xpath, name, class, nothing works.还尝试通过 xpath、名称、类直接转到选项,但没有任何效果。 I just cannot seem to click the option.我似乎无法单击该选项。 Don't know if it makes a difference here but I'm on a mac and using ChromeDriverManager.不知道这是否有所不同,但我在 Mac 上使用 ChromeDriverManager。 After clicking the option I have to click on 'Select' to go to the next page.单击该选项后,我必须单击“选择”才能转到下一页。

EDIT: Thanks Shreyas!编辑:谢谢Shreyas! Excuse my ignorance, I did that, and it does not bring an error, but nor do I see the option selected.请原谅我的无知,我这样做了,它不会带来错误,但我也没有看到选择的选项。 After clicking on the option I would have to click on a 'Select' button (tag 'input'), which I also cannot find with Selenium.单击该选项后,我必须单击“选择”按钮(标记“输入”),而 Selenium 也找不到该按钮。 Here is the HTML in case it helps:这是 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>
  1. First find the value of the option element you want to select.首先找到要选择的选项元素的值。
  2. Then you can run javascript to change the value of the select element.然后您可以运行 javascript 来更改 select 元素的值。
value = "B101"
driver.execute_script('var elements = document.getElementsByTagName("select");
                      for (element of elements) {
                        element.value = ' + value + '
                      }') 

document.getElementByTagNames('select') gets all the elements with the select tag and returns an array of all those elements document.getElementByTagNames('select')获取带有 select 标签的所有元素并返回所有这些元素的数组

Then, we loop through each element in the elements list and change the value of the select tag to your desired value.然后,我们遍历元素列表中的每个元素,并将 select 标记的值更改为您想要的值。

FOUND IT!找到了! Since the HTML has frames, first you have to switch to the correct frame:由于 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 ZA7F117F35426B9327 在复杂网站中找到下拉 select 元素和 select 选项 - How to locate the dropdown select element and select an option within a complex website using Selenium and Python select 使用 selenium 和 python 的选项 - select an option using selenium with python 如何使用硒(Python)从SELECT中选择一个选项? - How to select an option from SELECT using selenium (Python)? 如何使用 Selenium Python 在没有“选择”标签的下拉菜单中单击选项? - How to click option in dropdown menu which does not have "select" tag using Selenium Python? 如何在Python中使用Selenium从https://www.amazon.in/的下拉列表框中选择选项Books? - How to select the option Books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python? Python Selenium:选择选项 - Python Selenium: select option 使用 Python selenium 选择一个选项 - Select an option with Python selenium 如何使用 Selenium 和 Python 从自动建议中选择 select - How to select an option from the auto suggestions using Selenium and Python 如何使用Selenium和Python双击多选中的选项 - How to double click an option in a multi select using Selenium and Python 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from the dropdown menu using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM