简体   繁体   English

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

[英]Trouble choosing names from dropdown options

I've written some code in python in combination with selenium to select few options from three different dropdown boxes to reach the destination page. 我已经用python与硒结合编写了一些代码,以从三个不同的下拉框中选择一些选项以到达目标页面。 First off the browser is needed to click on a link in the first page to reach the page where dropdown item options are located. 首先,需要浏览器单击第一页上的链接才能到达下拉菜单选项所在的页面。 My script can click on the first page link but when it comes to choose the item from dropdown options then it fails to do that. 我的脚本可以单击第一页链接,但是从下拉选项中选择项目时,它将无法执行此操作。

Site address URL 网站地址网址

The link titled Search by Selecting a Plan or Network available in the first page (when clicked) leads to the second page where dropdown options can be found. 在第一页(单击时)中标题为Search by Selecting a Plan or Network的链接会转到第二页,在第二页中可以找到下拉选项。

Options I wanna choose from three different dropdowns (from second page): 我想从三个不同的下拉菜单中选择选项(第二页):

  1. Medical
  2. Arizona 亚利桑那
  3. All plans/networks 所有计划/网络

Here is what I've tried so far: 到目前为止,这是我尝试过的:

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()

Elements within which dropdown options are embedded: 嵌入下拉选项的元素:

</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>

Btw, when clicking on the first page link, it is necessary to wait few seconds so that the first page can load it's content. 顺便说一句,当单击首页链接时,需要等待几秒钟,以便首页可以加载其内容。

It seem that you're trying to handle wrong elements. 看来您正在尝试处理错误的元素。 Those select nodes are not what you actually need. 这些select节点不是您真正需要的。 You can do, for example, 您可以这样做,例如

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)

to see that it's totally different drop-down 看到完全不同的下拉菜单

Try below solution 尝试以下解决方案

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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