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

Ice*_*Tea 4 python selenium selenium-webdriver

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

超文本标记语言

    < 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>
Run Code Online (Sandbox Code Playgroud)

代码

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...
Run Code Online (Sandbox Code Playgroud)

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

有没有一种方法可以识别选项中的文本并单击它而不使用选择模块?或者有什么方法可以让选择模块在这种情况下工作?

Ice*_*Tea 5

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

然而,对我有用的是行动链。动作链之所以有效,是因为您不必定位某个元素。因此,在我的帖子中,我提到我可以单击下拉列表,但向下箭头不起作用,因为它给出了 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()
Run Code Online (Sandbox Code Playgroud)