无法单击selenium下拉列表中的选项

2024-06-25 23:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着使用Select模块,但是当我这样做时,元素要么是不可交互的,要么是“不可见的”。这是相关代码。在

HTML格式

    < head >
    < script >

        function onChangeCardType() {
            var value = $('#card_type').val();
            $('#img_' + value).siblings().hide();
            $('#img_' + value).show();
        }

    </script>

</head>

<body>
    <table>
        <thead>

            <tr>
                <th align="left">Card type</th>
                <td colspan="2" style="font-size:12px;">
                    <select name="requestDTO.vpc_card" id="card_type" onchange="onChangeCardType()"
                            class="select required"
                            style="width: 342px; font-size:12px;">
                        <option value="Amex" >American Express</option>
                        <option value="Mastercard" >MasterCard</option>
                        <option value="Visa" >Visa</option>
                        <option value="JCB" >JCB</option>
                    </select>
                    <a class="ui-selectmenu ui-widget ui-state-default select required ui-selectmenu-dropdown ui-corner-all" id="card_type_button_435" role="button" href="#" aria-haspopup="true" aria-owns="card_type_menu_435" aria-expanded="false" tabindex="0" style="width: 336px;"><span class="ui-selectmenu-status">Visa</span><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span></a>
                        <span class="ui-selectmenu-status">Visa</span>

                        <span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span>

                </td>
            </tr>

         </thead>
    </table>
</body>

代码

^{pr2}$

我还试着点击这个框(不使用select标记,我可以使用class="ui-selectmenu"单击它,但是.send_keys(KEYS.ARROW_DOWN)不起作用(给出一个AttributeError)。在

有没有一种方法可以在不使用选择模块的情况下识别选项中的文本并单击它?或者在这种情况下,有没有办法让Select模块工作?欢迎任何有启发性的回应!!在


Tags: 模块uivaluestyletypevisacardselect
3条回答

非常感谢你的回复!不幸的是,在这种情况下,问题并不在于等待:/

然而,对我有用的是行动链。动作链之所以有效,是因为您不必以元素为目标。所以在我的帖子中我提到我可以点击下拉列表,但是向下箭头不起作用,因为它给出了一个AttributeError。但是,那是因为我试着瞄准元素!在

所以我的答案是:

cardtype = driver.find_elements_by_class_name("ui-selectmenu-status")
cardtype.click()

actions = ActionChains(driver)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ENTER)
actions.perform()

您可以使用“按索引选择”。我个人推荐而不是价值

cardtype = Select(driver.find_element_by_id("card_type"))
cardtype.select_by_index(1)  // 0 - AMEX , 1 - MasterCard and so on

试试这个。在

your_choice=driver.find_element_by_xpath("//select[@id='card_type']/option[@value='Mastercard']")
your_choice.click()

相关问题 更多 >