Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-Selenium-从下拉菜单中选择一个元素_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python-Selenium-从下拉菜单中选择一个元素

Python-Selenium-从下拉菜单中选择一个元素,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我需要从下拉菜单中选择一个元素 例如: <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">

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

例如:

    <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>
但它不起作用。它会显示一条关于“元素不可见:元素当前不可见且可能无法操作”的消息

回溯(最近一次呼叫最后一次):
文件“scraping.py”,第23行,在
选择。通过可视文本(“所有时间”)选择
文件“D:\Python27\lib\site packages\selenium\webdriver\support\select.py”,第120行,在select\u by\u visible\u文本中
...
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见:元素当前不可见,不能进行操作

有什么想法吗?我试图从另一个stackoverflow问题中修复一些问题,但我没有找到解决方法

可以用显式等待替换隐式等待,显式等待直到元素在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()

可以用显式等待替换隐式等待,显式等待直到元素在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()

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

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

element.location_once_scrolled_into_view

希望这有帮助。谢谢。

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

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

element.location_once_scrolled_into_view

希望这有帮助。谢谢。

您试图从中选择的下拉列表实际上不是id为
rangeFilter
的元素。它位于同级

由于除了
之外,您不能在任何标记上使用
选择
类,因此您需要首先单击下拉列表以打开选项,然后单击选项

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()

您试图从中选择的下拉列表实际上不是id为
rangeFilter
的元素。它位于同级

由于除了
之外,您不能在任何标记上使用
选择
类,因此您需要首先单击下拉列表以打开选项,然后单击选项

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”数据切换=“dropdo wn”数据id=“rangeFilter”title=“查看依据”>。。。在点(182603)处不可单击。其他元素将收到点击:感谢Guy的帮助!我没有工作。。。我需要更多地了解硒。button type=“button”class=“btn dropdown toggle btn default”数据切换=“dropdo wn”数据id=“rangeFilter”title=“查看依据”>。。。在点(182603)处不可单击。其他元素将收到点击:感谢kiran的支持,但这对我不起作用。我也有同样的错误:selenium.common.exceptions.ElementNotVisibleException:Message:element不可见:element当前不可见,可能无法操作感谢kiran的支持,但它对我不起作用。我有相同的错误:selenium.common.exceptions.ElementNotVisibleException:Message:element not VisibleException:element当前不可见且可能无法操作页面是否滚动和下拉可见?否。我无法。。。。我的代码:driver=webdriver.Chrome(“D:\Python27\selenium\webdriver\Chrome\chromedriver.exe”)driver.implicitly\u wait(5)driver.maximize\u window()driver.get(“)driver.implicitly\u wait(5)element=driver.find\u element\u by\u xpath(//option[@value='all']))element.location一旦滚动到视图中,页面是否可见?否。我无法…我的代码:driver=webdriver.Chrome(“D:\Python27\selenium\webdriver\Chrome\Chrome\chromedriver.exe”)driver.implicitly\u wait(5)driver.maximize\u window()driver.get(“)driver.implicitly\u wait(5)element=driver.find\u元素\u by\xpath(//选项[@value='all']'))element.location\u一次将\u滚动到\u视图中