【问题标题】:Python - Selenium - select an element from a dropdown menuPython - Selenium - 从下拉菜单中选择一个元素
【发布时间】:2017-06-05 21:54:15
【问题描述】:

我需要从下拉菜单中选择一个元素。

例如:

    <div class="col-sm-4 col-lg-2">
       <label for="rangeFilter" class="sr-only">Date Range</label>
       <select class="selectpicker" id="rangeFilter" data-none-selected-text="Range" name="range">
              <option value="">View by</option>
              <option value="6month">6 months</option>
              <option value="1year">1 Year</option>
              <option value="2year">2 Year</option>
              <option value="all">All time</option>
         </select>
     </div>

但我总是有一些错误。

我的代码很简单:

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.implicitly_wait(5)
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")
time.sleep(15)
select = Select(driver.find_element_by_id('rangeFilter'))
select.select_by_visible_text('All time')

但它不起作用。出现有关“元素不可见:元素当前不可见并且可能无法操作”的消息

Traceback (most recent call last):
File "scraping.py", line 23, in <module>
select.select_by_visible_text('All time')
File "D:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
...
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated

有什么想法吗?我正在尝试从另一个 stackoverflow 问题中进行一些修复,但我没有找到方法......

【问题讨论】:

  • 你试过select_by_value吗?
  • 是的。我试过但我有同样的错误。谢谢!!!

标签: python selenium selenium-webdriver


【解决方案1】:

您尝试从中选择的下拉菜单实际上不是具有rangeFilter id 的元素。它在兄弟&lt;div&gt;中。

由于除了&lt;select&gt; 之外,您不能在任何标签上使用Select 类,您需要先单击下拉菜单以打开选项,然后单击选项

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.implicitly_wait(5)
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")

drop_down = driver.find_element_by_css_selector('[data-id="rangeFilter"]')
ActionChains(driver).move_to_element(drop_down).perform()
drop_down.click()

option = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//span[contains(., "All time")]')))
option.click()

【讨论】:

  • 感谢盖伊的帮助!我没有工作......我需要更多地了解硒。 button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdo wn" data-id="rangeFilter" title="View by">... 不可点击点 (182, 603)。其他元素会收到点击:
【解决方案2】:

除非我们向上滚动,否则您尝试交互的下拉菜单对用户不可见,这就是错误的原因。尝试向上滚动,然后与下拉菜单交互。下面的代码可能会给你一些想法。

element=find_element_by_xpath("xpath of the element you are trying to access")

element.location_once_scrolled_into_view

希望这会有所帮助。谢谢。

【讨论】:

  • 页面滚动和下拉可见吗?
  • 没有。我不能....我的代码: driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe") driver.implicitly_wait(5) driver.maximize_window() driver.get( "ispspeedindex.netflix.com/country/norway/") driver.implicitly_wait(5) element=driver.find_element_by_xpath("//option[@value='all']") element.location_once_scrolled_into_view
【解决方案3】:

您可以用显式等待替换隐式等待,显式等待会一直等到元素在 DOM 中呈现。

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

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")
try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "rangeFilter"))
    )
    select = Select(driver.find_element_by_id('rangeFilter'))
    select.select_by_visible_text('All time')
finally:
    driver.quit()

【讨论】:

  • 感谢 kiran 的支持,但这对我不起作用。我有同样的错误:selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见:元素当前不可见,可能无法操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 2020-05-14
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2022-11-11
相关资源
最近更新 更多