3

我尝试使用类似问题中提到的一些方法,但没有成功。在 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)
4

1 回答 1

3

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

您是否尝试过使用 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 语法文档

使用 None 时:元素被完全移除

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

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
于 2019-05-29T05:21:11.877 回答