简体   繁体   中英

Python: How to select option from drop down menu in Selenium, element hidden

I'm trying to fill out a form on this site: https://p2c.coweta.ga.us/p2c/jailinmates.aspx
(click "filter inmate list", then click + button to add a row)

def coweta_search(last, first):
    print("Coweta County Jail")
    url = "https://p2c.coweta.ga.us/p2c/jailinmates.aspx"
    driver = webdriver.Chrome(chrome_options=(), executable_path="/usr/bin/chromedriver")
    wait = WebDriverWait(driver, 30)
    driver.get(url)
    driver.find_element_by_css_selector("#btnGridSearch > span:nth-child(2)").click()
    driver.find_element_by_css_selector(".field0 > option:nth-child(7)").click()
    driver.find_element_by_css_selector("input.default").send_keys(last)
    driver.find_element_by_css_selector(".ui-icon-plusthick").click()
    driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(1) > select:nth-child(1) > option:nth-child(2)").click()

    driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select:nth-child(2) > option:nth-child(7)").click()


    return driver

When I run the code as shown the second drop down menu on the second row seems to be hidden. the stack trace returns:

element not interactable: Element is not currently visible and may not be manipulated 

I've tried different things to work around this, such as explicit waits, or wait until element visible, but none of that worked.

I also have tried driver.execute_script:

first_drop = driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select:nth-child(2) > option:nth-child(7)")
driver.execute_script("arguments[0].click();", first_drop)

This didn't give an error, but it didn't actually select the option( perhaps because its hidden?).

Is there a way I can select the option im trying to get that is being hidden?

It might be worth noting the same drop down menu has a default class that seems like it might be laying on top or something.

I managed to get it working by changing the last line to:

driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select.field1 > option:nth-child(7)").click()

I believe the selector that you have provided is not unique and selenium is getting confused.

You can also get the selectors directly by right clicking the element in the inspection menu and selecting copy selector .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM