Home > other >  Selenium unable to locate or interact with element in dropdown menu
Selenium unable to locate or interact with element in dropdown menu

Time:04-01

I'm attempting to select a county in Florida from the dropdown accessed here, but nothing I've tried so far has been able to locate or interact with any of the elements from the page. Generally, I get the error:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//select[@id='form:county_input']//option[value='24']"}
  (Session info: chrome=100.0.4896.60)

This appears to be the relevant snippet:

<div id="form:county"  style="width:500px;" role="combobox" aria-haspopup="true" aria-expanded="false" aria-owns="form:county_items">
    <div >
        <input id="form:county_focus" name="form:county_focus" type="text" autocomplete="off" aria-expanded="false" aria-labelledby="form:j_idt20" aria-autocomplete="list" aria-activedescendant="form:county_0" aria-describedby="form:county_0" aria-disabled="false">
    </div>
    <div >
        <select id="form:county_input" name="form:county_input" tabindex="-1" aria-hidden="true">
            <option value="02" data-escape="true">BAKER COUNTY CLERK OF COURT</option>
            <option value="04" data-escape="true">BRADFORD COUNTY CLERK OF COURT</option>
            <option value="07" data-escape="true">CALHOUN COUNTY CLERK OF COURT</option>
            <option value="12" data-escape="true">COLUMBIA COUNTY CLERK OF COURT</option>
            <option value="14" data-escape="true">DESOTO COUNTY CLERK OF COURT</option>
            <option value="15" data-escape="true">DIXIE COUNTY CLERK OF COURT</option>
            <option value="19" data-escape="true">FRANKLIN COUNTY CLERK OF COURT</option>
            <option value="21" data-escape="true">GILCHRIST COUNTY CLERK OF COURT</option>
            <option value="22" data-escape="true">GLADES COUNTY CLERK OF COURT</option>
            <option value="23" data-escape="true">GULF COUNTY CLERK OF COURT</option>
            <option value="24" data-escape="true">HAMILTON COUNTY CLERK OF COURT</option>
        </select>
    </div>
    <label id="form:county_label" >BAKER COUNTY CLERK OF COURT</label>

And here's my code, where I try to select Hamilton County:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common import action_chains

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
browser = webdriver.Chrome(filepath   "chromedriver.exe", options = options)

browser.get("https://www.civitekflorida.com/ocrs/app/search.xhtml")
county = browser.find_element_by_xpath(".//select[@id='form:county_input']//option[value='24']")

It's a dropdown menu that I'm trying to interact with, so maybe there's someway to expand and then select?

CodePudding user response:

The element you searching for only appears after click in dropdown button try to click on the dropdown before click on the option

try this code:


browser.get("https://www.civitekflorida.com/ocrs/app/search.xhtml")
browser.find_element_by_xpath("//*[@id='form:county']/div[3]").click()
time.sleep(0.5)
county = browser.find_element_by_id("form:county_24")
print(county.text)

Output: OKEECHOBEE COUNTY CLERK OF COURT

  • Related