【问题标题】:Python Selenium Webdriver Select Dropdown ValuePython Selenium Webdriver 选择下拉值
【发布时间】:2016-11-23 09:11:58
【问题描述】:

我正在尝试自动化此网站中的搜索过程:https://www.bcbsga.com/health-insurance/provider-directory/searchcriteria 该过程涉及单击“继续”按钮以在“访客”模式下进行搜索。下一页有一个下拉项目列表,用于细化搜索条件。我的代码要么产生“元素不可见”异常(我通过等待来纠正)要么超时。请帮忙。

这是我的代码:

# navigate to the desired page
driver.get("https://www.bcbsga.com/health-insurance/provider-directory/searchcriteria")
# get the guest button
btnGuest = driver.find_element_by_id("btnGuestContinue")
#click the guest button
btnGuest.click()
wait = WebDriverWait(driver,10)
#Find a Doctor Search Criteria page
element = wait.until(EC.visibility_of_element_located((By.ID,"ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance")))
lstGetInsurance = Select(element)
lstGetInsurance.select_by_value("BuyMyself$14States")

# close the browser window
#driver.quit()

【问题讨论】:

    标签: python selenium-chromedriver


    【解决方案1】:

    你可以使用输入搜索和key.return:

    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    divID = 'ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance_chosen'
    inputID = 'ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance_chosen_input'
    inputValue = 'I buy it myself (or plan to buy it myself)'
    
    driver = webdriver.Chrome()
    
    driver.get("https://www.bcbsga.com/health-insurance/provider-directory/searchcriteria")
    driver.find_element_by_id("btnGuestContinue").click()
    driver.implicitly_wait(10)
    driver.find_element_by_id(divID).click()
    driver.find_element_by_id(inputID).send_keys(inputValue)
    driver.find_element_by_id(inputID).send_keys(Keys.RETURN)
    time.sleep(6)
    driver.close()
    

    【讨论】:

    • 非常感谢您的解决方案 - 它有效!我想知道我之前的代码有什么问题。任何指针?我什至尝试通过可见文本进行选择。
    • 您使用的 ID 是 'select' id,但选择存储在 'input' 类型中,当您被选中时会出现文本。
    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 2016-12-31
    • 1970-01-01
    • 2015-05-19
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多