【问题标题】:Python Selenium selecting option using Select (element not visible?)使用 Select 的 Python Selenium 选择选项(元素不可见?)
【发布时间】:2019-05-29 05:00:39
【问题描述】:

我尝试使用类似问题中提到的一些方法,但没有成功。在 HTML 源代码中显然有“值”和“文本”属性,但是当我使用 selenium.webdriver 访问这些属性时,我似乎无法访问这些属性?

注释选择导致页面数据发生变化...


编辑2:

Guy 在下面指出,实际的下拉菜单可能是一个元素而不是元素。但是使用 el.click() 只会闪烁并且不会打开下拉菜单。


EDIT1: 现在可以识别 and 元素,但我无法进行选择。我相信该页面也在 javascript 中,因此我不确定这是否会影响使用的方法。


原帖:

网页: https://www.racv.com.au/on-the-road/driving-maintenance/fuel-prices.html

选择的HTML代码,为了可见性,省略了一些选项:

<select name="filter-select-6" id="filter-select-6" class="js-dropdown js-select-map js-filter-select" data-filter="#filter-list-60 .js-tab-item" data-url="/bin/racv/fuelprice" style="display: none;" data-parsley-id="3">                            
    <option value="11" data-index="0">LRP</option>
    <option value="2" selected="true" data-index="0">Unleaded</option>
    <option value="3" data-index="0">Diesel</option>
    <option value="8" data-index="0">Premium Unleaded 98</option>
</select>

我相信我可以毫无问题地选择 Select 元素:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

url = 'https://www.racv.com.au/on-the-road/driving-maintenance/fuel-prices.html'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(20)

fuel_select = Select(driver.find_element_by_id('filter-select-6'))

当我打印选项时,我得到:

for fuel_option in fuel_select.options:
    print(fuel_option)

<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-2")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-3")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-8")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-11")>

使用选择():

for fuel_option in fuel_select.find_elements_by_tag_name('option'):
    if fuel_option.text == "Diesel":
        fuel_option.select()

错误:

Traceback (most recent call last):
  File "C:/file.py", line 18, in <module>
    fuel_option.Select()
AttributeError: 'WebElement' object has no attribute 'select'

使用 click() 或使用任何 select_by_xxx():

for fuel_option in fuel_select.find_elements_by_tag_name('option'):
    if fuel_option.text == "Diesel":
        fuel_option.click()

#or using select_by_xxx
fuel_select.select_by_value('8')

错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

【问题讨论】:

  • 你能导航到那个下拉菜单吗?该下拉菜单位于 div 元素内。该 Div 具有名为 hidden 的属性。
  • 当然,我刚刚检查了默认为“UNLEADED”的下拉菜单。我相信它应该是 class="hidden" 正下方的
    ,它在
    里面

标签: python selenium


【解决方案1】:

Select 是 WebElement 的包装器,select() 不是有效方法。请参考Select doc

您是否尝试过使用 select_by_value:

fuel_select = Select(driver.find_element_by_id('filter-select-6'))
fuel_select.select_by_value("8")

或通过可见文本:

fuel_select = Select(driver.find_element_by_id('filter-select-6'))
fuel_select.select_by_visible_text("Premium Unleaded 98")

EDIT1
先尝试 click() 使下拉菜单可见:

el = driver.find_element_by_id('filter-select-6')
el.click()
fuel_select = Select(el)

EDIT2:
我相信您的问题与您使用 css 属性 style="display: none;" 的事实更相关 您也应该无法手动查看下拉菜单。

详情请参考css syntax doc

当使用无时:元素被完全移除

可能不是“理想的”,但您可以更改此属性的值以使其再次可见:

driver.execute_script('arguments[0].style.display = "block";', el)

代码如下所示:

el = driver.find_element_by_id('filter-select-6')
driver.execute_script('arguments[0].style.display = "block";', el)

fuel_select = Select(el)
fuel_select.select_by_value("8")

EDIT3:
刚刚注意到您提供了该网站!很有用。 所以下拉菜单作为另一个元素被隐藏起来,只有在点击后才可用。

这是代码,它对我有用

from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.support.ui import Select

# Get the first element and tap on it, note you might have to tap few time.
el = driver.find_element_by_css_selector('.chosen-single > div')
action = TouchActions(driver)
action.tap(el).perform()

# once the dropdown is open it does not seems that the Select el is the one to use
els = driver.find_elements_by_css_selector('.active-result')
for el in els:
    if el.text == 'Diesel':
        el.click()
        break

【讨论】:

  • 感谢尼克的回复。刚刚尝试了您的建议并返回相同的错误:'Traceback(最近一次调用最后一次):文件“file.py”,第 16 行,在 fuel_select.select_by_value(“8”)'selenium.common.exceptions.ElementNotVisibleException:消息:元素不可交互:元素当前不可见,可能无法操作(会话信息:chrome=74.0.3729.169)(驱动程序信息:chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29} ),平台=Windows NT 10.0.17134 x86_64)
  • 如果你先点击然后使用选择,你会得到相同的结果吗?尝试 el = driver.find_element_by_id('filter-select-6'), el.click(). 然后将 el 分配给 Select fuel_select = Select(el)
  • 我认为click() 只是webdriver 的一种方法,而不是Select(webriver.find_element_by_id() 的一种方法? fuel_select.click() 正在返回 AttributeError: 'Select' object has no attribute 'click'
  • 正确你需要点击元素,我已经编辑了代码以获得更多可见性
  • 这仍然是selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipulated。这可能与另一个
    相关,它在同一
猜你喜欢
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多