无法从下拉选项中选择名称

时间:2017-12-05 07:00:22

标签: python python-3.x selenium selenium-webdriver web-scraping

我已经在python中编写了一些与selenium结合使用的代码,从三个不同的下拉框中选择几个选项来到达目标页面。首先需要在浏览器中单击第一页中的链接以访问下拉项选项所在的页面。我的脚本可以单击第一页链接,但是当从下拉选项中选择项目时,它就无法执行此操作。

网站地址URL

第一页(点击时)中标题为Search by Selecting a Plan or Network的链接指向第二页,其中可以找到下拉选项。

选项我想从三个不同的下拉菜单中选择(来自第二页):

  1. 医疗
  2. 亚利桑那
  3. 所有计划/网络
  4. 这是我到目前为止所尝试的内容:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select
    
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver, 20)
    driver.get("use_above_link")
    wait.until(EC.presence_of_element_located((By.ID, "lnkGuestPlanSearch"))).click()
    
    Select(driver.find_element_by_name('ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlPlanC')).select_by_visible_text('Medical')
    Select(driver.find_element_by_name('ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlState')).select_by_visible_text('Arizona')
    Select(driver.find_element_by_name('ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlPlanN')).select_by_visible_text('All plans/networks')
    
    driver.quit()
    

    嵌入下拉选项的元素:

    </select><div class="chosen-container chosen-container-single pf-field-320-30" style="width: 0px;" title="" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanC_chosen"><a class="chosen-single"><span tabindex="0">Enter or select type of care</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanC_chosen_input" type="text" autocomplete="off" title="auto"></div><ul class="chosen-results"></ul></div></div>
        </div>
    
        <div id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_divMedicalCareText" style="display:none">
           <table class="AlertBox" style="width:320px"><tbody><tr>
             <td class="AlertIcon"> <img alt="Find more information about Behavioural Health Providers" src="https://www.providerfinder.anthem.com/images/ProviderFinder/SearchResults/15_Info.png"></td>
             <td class="AlertBoxText" style="width:298px">Providers for Behavioral Health &amp; Substance Use Disorder Services are listed under Medical Care.</td></tr>
          </tbody></table> 
        </div>
    
        <div id="divState" class="paddingtop35 paddingbottom20" style="display: block;">
        <div class="headerstyle-item">
            <label for="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlState_chosen_input" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_Label1">What state do you want to search in?</label>
        </div>
        <select name="ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlState" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlState" tabindex="0" class="pf-field" aria-labelledby="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelectPlanUnkwn_Label1" disabled="" style="display: none;">
        <option value="Enter or select a state">Enter or select a state</option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
    </select>
    
    <div class="chosen-container chosen-container-single pf-field-320-30 chosen-disabled" style="width: 0px;" title="" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlState_chosen"><a class="chosen-single"><span tabindex="0">Enter or select a state</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlState_chosen_input" type="text" autocomplete="off" title="auto" disabled=""></div><ul class="chosen-results"></ul></div></div>
        </div>
    
        <div class="headerstyle-item paddingtop35" id="divPlanName">
                <label for="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanN_chosen_input" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_Label6">Select a plan/network</label>
        </div>
        <div id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_divPlanFocus" class="paddingbottom20">
            <select name="ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlPlanN" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanN" class="pf-field" aria-labelledby="ctl00_MainContent_maincontent_pfPlanSelection_Label2" disabled="" style="display: none;"><option value="">Select a plan/network</option></select><div class="chosen-container chosen-container-single pf-field-320-30 chosen-disabled" style="width: 0px;" title="" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanN_chosen"><a class="chosen-single"><span tabindex="0">Select a plan/network</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_ddlPlanN_chosen_input" type="text" autocomplete="off" title="auto" disabled=""></div><ul class="chosen-results"></ul></div></div>
        </div>
    
        <div class="paddingtop50 paddingbottom50">
            <span>
                <input type="submit" name="ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$btnPlanCancel" value="Cancel" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_btnPlanCancel" class="secondaryButton buttonwidth150">
            </span>
              <span>
               <input type="submit" name="ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$btnPlanContinue" value="Continue" id="ctl00_MainContent_maincontent_pfSearchMain_pfSelfPlanSelection_btnPlanContinue" class="primaryButton buttonwidth150">
            </span>
        </div>
    </div>
    

    顺便说一句,点击第一页链接时,需要等待几秒钟,以便第一页可以加载它的内容。

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试处理错误的元素。那些select节点不是您真正需要的节点。例如,你可以做到

type_of_care = driver.find_element_by_name('ctl00$MainContent$maincontent$pfSearchMain$pfSelfPlanSelection$ddlPlanC')
driver.execute_script('arguments[0].style.display="block";', type_of_care)

看到它完全不同的下拉列表

尝试以下解决方案

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get("https://www.anthem.com/health-insurance/provider-directory/searchcriteria")
wait.until(EC.presence_of_element_located((By.ID, "lnkGuestPlanSearch"))).click()

type_of_care = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Enter or select type of care')))
type_of_care.click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//li[.="Medical"]'))).click()

state = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Enter or select a state')))
state.click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//li[.="Arizona"]'))).click()

network_plan = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Select a plan/network')))
network_plan.click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//li[.="All plans/networks"]'))).click()