使用Select的Python Selenium选择选项(元素不可见?)

时间:2019-05-29 05:00:40

标签: python selenium

我尝试使用在类似问题中提到的一些方法,但是没有运气。在HTML源代码中,显然有'value'和'text'属性,但是当我使用selenium.webdriver访问这些属性时,我似乎无法访问它们?

注释选择导致页面上数据的更改...


编辑2:

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


EDIT1: 现在可以识别和元素,但我无法进行选择。我认为该页面也使用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")>

使用select():

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)

2 个答案:

答案 0 :(得分:2)

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
尝试先单击()以使下拉菜单可见:

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

答案 1 :(得分:0)

实际的下拉列表是html树中<select>下的另一个元素。这个不是<select>,因此您必须单击它以打开选项,然后单击要寻找的那个

# open the dropdown
driver.find_element_by_class_name('chosen-single').click()

# click on Diesel option
driver.find_element_by_xpath('//li[.="Diesel"]').click()