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

标签 python selenium

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

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

<小时/>

编辑2:

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

<小时/>

编辑1: 现在可以识别 和 元素,但我无法做出选择。我相信该页面也是用 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)

最佳答案

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

编辑1
首先尝试单击()以使下拉列表可见:

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

编辑2:
我相信您的问题与您使用 css 属性 style="display: none;" 您也不应该能够手动看到下拉菜单。

请引用css syntax doc了解更多详情

使用 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")

编辑3:
刚刚注意到您提供了网站!很有用。 因此,下拉列表被隐藏为另一个元素,并且只有在单击后才可用。

这是代码,它对我有用

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

关于Python Selenium 使用 Select 选择选项(元素不可见?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353376/

相关文章:

python - celery block 内链

python - 如何在 Tkinter 小部件中获取 cmd

python - 如何在线性回归中强制零截距?

javascript - 使用 BeautifulSoup 从图像标签 Src 属性中提取 JPG

java - Selenium 结构化模式java

java - Selenium 不会通过 xpath 查找值大于 99 的元素

python - 在分层数据框上添加带有 groupby 的列

c# - 我怎样才能让 Selenium-WebDriver 在发送 key 后等待几秒钟?

javascript - 如何使用 Cmder 在 Windows 计算机上运行时 (process.env.SERVER) 运行 npm 变量?

java - selenium 可以处理自动完成吗?