【问题标题】:Selenium Python - cannot select options selection boxSelenium Python - 无法选择选项选择框
【发布时间】:2021-12-06 17:36:20
【问题描述】:

一个关于股票的网站有一个漂亮的图表,其中有几个显示图表的选项(请参阅https://www.boerse.de/chart-tool/HeidelbergCement-Aktie/DE0006047004)。我总是需要不同股票的图表,但图表的设置总是相同的,所以我想自动选择图表的设置。例如,我总是想在图表类型<select id = "types" 的选择框中选择选项“条形图”(<option value = "Balken"> Bar-Chart </option>)。但我无法控制图表中的选项。可能是因为它是一个多层选择框。

在这方面,我的(不工作的)代码是:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
driver=webdriver.Chrome()
url ="https://www.boerse.de/chart-tool/HeidelbergCement-Aktie/DE0006047004"
driver.get(url)
time.sleep(10)
target = driver.find_element_by_class_name("box_full")
target.location_once_scrolled_into_view
driver.maximize_window()
select = Select(driver.find_element(By.ID,"types"))
select.select_by_value(“Balken”) # that doesn’t work

我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/peter/Desktop/Python2020/Selenium/Selenium02.py", line 52, in <module>
    select.select_by_value('Balken')
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-Charttyp <select id="types")  packages\selenium\webdriver\support\select.py", line 82, in select_by_value
    self._setSelected(opt)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
    option.click()
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 693, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=94.0.4606.81)

【问题讨论】:

    标签: python selenium select


    【解决方案1】:

    "element not intractable" 错误通常表示该元素不可见或上面有其他元素,因此 selenium 无法与之交互。

    解决方案是检查#types 元素在您尝试与之交互时是否在屏幕上实际可见。

    您也可以通过纯 JavaScript 更改选择值,使用 execute_script,但这可能会有不触发本应触发的事件的缺点。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2020-05-15
    • 2019-04-04
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    相关资源
    最近更新 更多