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

国际教育培训中心

我尝试使用“选择”模块,但是当我执行此操作时,元素要么不可交互,要么“不可见”。以下是相关代码。

的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>

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import Select

#testing on a website that's not public yet, so I won't show the set-up here,but if it's required I can too

cardtype = Select(driver.find_element_by_id("card_type"))
cardtype.select_by_value("Mastercard")
sleep(1)
driver.implicitly_wait(2)


Using Firefox: ElementNotInteractableException:
Element <option> could not be scrolled into view


Using Chrome:
ElementNotVisibleException: element not visible: 
Element is not currently visible and may not be manipulated

# sleep nor implicitly_wait doesn't help too...

我也尝试仅单击该框(不使用select标记,我可以使用来单击它class="ui-selectmenu",但.send_keys(KEYS.ARROW_DOWN)不起作用(给出AttributeError)。

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

国际教育培训中心

非常感谢您的答复!不幸的是,在这种情况下,问题实际上不是等待:/

但是对我有用的是动作链。动作链之所以起作用,是因为您不必定位元素。所以在我的帖子中我提到我可以单击下拉列表,并且向下箭头不起作用,因为它给出了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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将通过搜索文本字段生成的下拉列表项存储在硒列表中

无法从硒的下拉列表中选择选项(尝试了所有方法)

无法选择“用硒选择”中的选项

硒:无法在iframe中单击按钮

Nightwatch无法找到/单击下拉选项

硒通过下拉菜单中的选项进行迭代

如何从长下拉列表中单击使用硒的元素?

VBA:从网站的下拉列表中单击选项

单击使用硒的下拉列表中的所有值

无法单击硒中的元素

无法在选项(“角度”下拉列表中)中选择值。

根据选择下拉列表中的单击选项更改标记

硒无法单击iframe中的元素

单击硒中的select2下拉列表时出现问题

我在使用硒在C#中单击时遇到问题打开下拉选择选项

无法从iframe中的下拉菜单中选择选项-硒-Java8

无法使用硒与javascript和jest framewok在自动化中关闭cdk下拉列表

硒选择选项的下拉列表

python硒无法单击下拉菜单中的隐藏元素

如何单击所选项目,然后导航至p下拉列表中的相应组件?

动态微调-无法单击下拉列表-Android

JavaScript无法从选择下拉列表中检索选项的值

无法单击硒中的列表按钮选项:异常元素不可见

无法在<select>中选择其他选项,但是选项列表在下拉列表中可见...

无法从硒中的日、月和年下拉选项的下拉列表中选择值

单击 ENTER 时无法从下拉列表中获取值

如何使用硒从下拉列表中选择选项?

如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项

NetSuite 自动显示列表后,无法单击下拉列表选项