Python Selenium Webdriver - 从网站检索顺序数据

时间:2016-11-23 13:09:59

标签: python selenium selenium-webdriver

我试图从https://www.bcbsga.com/health-insurance/provider-directory/searchcriteria检索医生姓名列表,地址和其他字段。我在下面附上了我的代码。我不确定' for'是什么问题。最后循环,我得到类似下面的输出而不是网站上显示的数据。

selenium.webdriver.remote.webelement.WebElement (session =" ba8c85ad6e398939063d5b43ee513668",element =" 0.6436841330730552-19")> selenium.webdriver.remote.webelement.WebElement(session =" ba8c85ad6e398939063d5b43ee513668",element =" 0.6436841330730552-20")>

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('ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance_chosen').click()
driver.find_element_by_id('ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance_chosen_input').send_keys('I buy it myself (or plan to buy it myself)')
driver.find_element_by_id('ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireInsurance_chosen_input').send_keys(Keys.RETURN)

driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireWhobought_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireWhobought_chosen_input").send_keys("Marketplace/exchanges")
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireWhobought_chosen_input").send_keys(Keys.RETURN)

driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireState_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireState_chosen_input").send_keys("Georgia")
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlQuestionnaireState_chosen_input").send_keys(Keys.RETURN)

driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlCareTypes_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlCareTypes_chosen_input").send_keys("Medical")
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlCareTypes_chosen_input").send_keys(Keys.RETURN)

driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlquestionnairePlanNetworks_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlquestionnairePlanNetworks_chosen_input").send_keys("All plans/networks selection")
driver.find_element_by_id("ctl00_MainContent_maincontent_PFPlanQuestionnaire_ddlquestionnairePlanNetworks_chosen_input").send_keys(Keys.RETURN)

driver.find_element_by_id("btnGuestQuestionnaireContinue").click()

#'located near' field
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_pfLocationSearch_txtZipCityState").send_keys("30303")

#within a distance of
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_pfLocationSearch_ddlRadius_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_pfLocationSearch_ddlRadius_chosen_input").send_keys("10 miles")
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_pfLocationSearch_ddlRadius_chosen_input").send_keys(Keys.RETURN)
#speciality
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_ddlSpeciality_chosen").click()
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_ddlSpeciality_chosen_input").send_keys("Cardiology")
driver.find_element_by_id("ctl00_MainContent_maincontent_pfSearchMain_ddlSpeciality_chosen_input").send_keys(Keys.RETURN)
#click the search button
driver.find_element_by_id("btnSearch").click()
time.sleep(10)

for i in range(10):
        name = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lnkName")
        address = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lblAddress")
        print(name, address)

driver.close()

2 个答案:

答案 0 :(得分:1)

我想我最后在'for'循环中遗漏了.text字段。添加它们会检索元素的名称!

for i in range(10):
        name = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lnkName").text
        address = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lblAddress").text
        print(name, address)

答案 1 :(得分:1)

问题是find_element_by_id应该返回web元素,而不是文本节点,所以显然nameaddress是web元素。你应该使用

for i in range(10):
    name = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lnkName").text
    address = driver.find_element_by_id("ctl00_ResultsList_ctrl" + str(i) + "_lblAddress").text
    print(name, address)