簡體   English   中英

Python:如何從Selenium的下拉菜單中選擇選項,元素已隱藏

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

我正在嘗試在此網站上填寫表格: https//p2c.coweta.ga.us/p2c/jailinmates.aspx
(單擊“過濾囚犯列表”,然后單擊+按鈕添加一行)

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

當我如圖所示運行代碼時,第二行的第二個下拉菜單似乎被隱藏了。 堆棧跟蹤返回:

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

我嘗試了其他方法來解決此問題,例如顯式等待,或等到元素可見為止,但是這些都不起作用。

我也嘗試過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)

這沒有給出錯誤,但是實際上沒有選擇該選項(也許是因為它隱藏了?)。

有沒有辦法我可以選擇隱藏的選項?

可能值得一提的是,同一下拉菜單中的默認類似乎位於頂部或其他位置。

我設法通過將最后一行更改為:

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

我相信您提供的選擇器不是唯一的,並且硒越來越混亂。

您還可以通過右鍵單擊檢查菜單中的元素並選擇“ copy selector器”來直接獲得選擇copy selector

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM