Home > Back-end >  Unable to read option names from drop down menu
Unable to read option names from drop down menu

Time:03-08

I am trying read name of selected option from marked drop downs so that based on selected name I can make code take certain actions. Problem is it doesn't allow me to read it(I am using selenium for it). Code line I have written(Available below image) throws an exception, so not sure how to tackle it.

website link : enter image description here

CODE LINE:

print(driver.find_element(By.XPATH,'//*[@id="select2-mcw_mtr-fl-container"]/div/div[1]'))

EXCEPTION:

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]

CodePudding user response:

As far as I can see this website uses dynamic XPaths, but first part of the path seems to always stay the same.

So we can do a partial search. Try this:

print(driver.find_element(By.XPATH, """//*[contains(@id, "select2-mcw")]""").text)
  • Related