selenium.common.exceptions.ElementNotInteractableException:消息:元素<option>无法滚动到视图中

时间:2020-04-12 14:42:04

标签: python selenium

我在预订网站上有一些带有下拉列表的HTML,我试图使用Selenium单击选项组中的值,但得到

selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view

HTML

<!DOCTYPE html>
<html lang="en" data-asset-path="/" data-domain="test_domain.com">
  <head>
    Hello
  </head>

  <h1>Test page</h1>

  <div class="player-select flex-grow relative">
  <select id="member_booking_form_player_2" name="member_booking_form[player_2]"
    ><option value="">Start typing to find player...</option
    ><optgroup label="General">
      <option value="-2">Guest</option
      ><option value="-3">Member not in list</option></optgroup
    ><optgroup label="You and your buddies">
      <option value="349">Player, One</option></optgroup
    ><optgroup label="Other club members">
      <option value="431">Fictional, Person</option
      ><option value="1846">Someone, Madeup</option></optgroup
    ></select>
</div>
</html>

功能

    def book_a_player_test(self, player_id, surname, first):

        # Use actionchains as it at least interacts with the dropdown
        player = self.browser.find_element_by_xpath("//select[@id='member_booking_form_player_2'][@name='member_booking_form[player_2]']")
        ActionChains(self.browser).move_to_element(player).click().perform()  

        # Try to pick out the element
        select = Select(self.browser.find_element_by_id('member_booking_form_player_2'))
        #ways to handle drop down
        select.select_by_visible_text("Player, One")

注意-香港专业教育学院试图将动作链与SELECT功能结合起来,但没有任何效果:-(

我在Mac Catalina上使用带有最新geckodriver的Firefox

2 个答案:

答案 0 :(得分:1)

尝试以下解决方案来解决您的问题,其中下拉菜单中的下拉菜单选项无法进入滚动视图。

解决方案1:

selectElement = browser.find_element_by_id('member_booking_form_player_2')

ActionChains(self.browser).move_to_element(selectElement).perform()
dropdown = Select(selectElement)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='member_booking_form_player_2']//options[contains(.,'Fictional, Person')]"))).click()

注意::请在您的解决方案中添加以下内容

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

答案 1 :(得分:0)

感谢您的帮助,结合了下拉菜单和选项组的动作链,以及Select_by_Visible。

    def book_a_player_test(self, player_id, surname, first):
        # Located and open the DropDown menu
        player = self.browser.find_element_by_xpath("//select[@id='member_booking_form_player_2'][@name='member_booking_form[player_2]']/optgroup[@label='Other club members']")
        ActionChains(self.browser).move_to_element(player).click().perform()

        # Select the Element from the OptionGroup
        select = Select(self.browser.find_element_by_id('member_booking_form_player_2'))
        select.select_by_visible_text("Player, One")