无法从下拉菜单中读取选项名称
Unable to read option names from drop down menu
我正在尝试从标记的下拉列表中读取所选选项的名称,以便根据所选名称让代码执行某些操作。问题是它不允许我阅读它(我正在使用硒)。我写的代码行(下图可用)抛出异常,所以不知道如何解决它。
网站 link : https://mcchip-dkr.com/en/chiptuning-database?fid=1f2zc6cezk&view=carselector#0
代码行:
print(driver.find_element(By.XPATH,'//*[@id="select2-mcw_mtr-fl-container"]/div/div[1]'))
异常:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="select2-mcw_mtr-fl-container"]/div/div[1]"}
(Session info: chrome=99.0.4844.51)
Stacktrace:
Backtrace:
Ordinal0 [0x00D269A3+2582947]
Ordinal0 [0x00CBA6D1+2139857]
Ordinal0 [0x00BB3A98+1063576]
Ordinal0 [0x00BDFF3E+1244990]
Ordinal0 [0x00BE013B+1245499]
Ordinal0 [0x00C09F52+1417042]
Ordinal0 [0x00BF8594+1344916]
Ordinal0 [0x00C0834A+1409866]
Ordinal0 [0x00BF8366+1344358]
Ordinal0 [0x00BD5176+1200502]
Ordinal0 [0x00BD6066+1204326]
GetHandleVerifier [0x00ECBE02+1675858]
GetHandleVerifier [0x00F8036C+2414524]
GetHandleVerifier [0x00DBBB01+560977]
GetHandleVerifier [0x00DBA8D3+556323]
Ordinal0 [0x00CC020E+2163214]
Ordinal0 [0x00CC5078+2183288]
Ordinal0 [0x00CC51C0+2183616]
Ordinal0 [0x00CCEE1C+2223644]
BaseThreadInitThunk [0x759EFA29+25]
RtlGetAppContainerNamedObjectPath [0x77947A9E+286]
RtlGetAppContainerNamedObjectPath [0x77947A6E+238]
据我所知,该网站使用动态 XPath,但路径的第一部分似乎始终保持不变。
所以我们可以进行部分搜索。试试这个:
print(driver.find_element(By.XPATH, """//*[contains(@id, "select2-mcw")]""").text)
我正在尝试从标记的下拉列表中读取所选选项的名称,以便根据所选名称让代码执行某些操作。问题是它不允许我阅读它(我正在使用硒)。我写的代码行(下图可用)抛出异常,所以不知道如何解决它。
网站 link : https://mcchip-dkr.com/en/chiptuning-database?fid=1f2zc6cezk&view=carselector#0
代码行:
print(driver.find_element(By.XPATH,'//*[@id="select2-mcw_mtr-fl-container"]/div/div[1]'))
异常:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="select2-mcw_mtr-fl-container"]/div/div[1]"}
(Session info: chrome=99.0.4844.51)
Stacktrace:
Backtrace:
Ordinal0 [0x00D269A3+2582947]
Ordinal0 [0x00CBA6D1+2139857]
Ordinal0 [0x00BB3A98+1063576]
Ordinal0 [0x00BDFF3E+1244990]
Ordinal0 [0x00BE013B+1245499]
Ordinal0 [0x00C09F52+1417042]
Ordinal0 [0x00BF8594+1344916]
Ordinal0 [0x00C0834A+1409866]
Ordinal0 [0x00BF8366+1344358]
Ordinal0 [0x00BD5176+1200502]
Ordinal0 [0x00BD6066+1204326]
GetHandleVerifier [0x00ECBE02+1675858]
GetHandleVerifier [0x00F8036C+2414524]
GetHandleVerifier [0x00DBBB01+560977]
GetHandleVerifier [0x00DBA8D3+556323]
Ordinal0 [0x00CC020E+2163214]
Ordinal0 [0x00CC5078+2183288]
Ordinal0 [0x00CC51C0+2183616]
Ordinal0 [0x00CCEE1C+2223644]
BaseThreadInitThunk [0x759EFA29+25]
RtlGetAppContainerNamedObjectPath [0x77947A9E+286]
RtlGetAppContainerNamedObjectPath [0x77947A6E+238]
据我所知,该网站使用动态 XPath,但路径的第一部分似乎始终保持不变。
所以我们可以进行部分搜索。试试这个:
print(driver.find_element(By.XPATH, """//*[contains(@id, "select2-mcw")]""").text)