使用Selenium和BeautifulSoup提取iFrame内容

时间:2019-03-26 03:54:10

标签: python selenium iframe beautifulsoup

抱歉,这是我的第一篇文章,所以请原谅我所不知道的所有内容!谢谢。

我正在尝试填写以下表格并提取相关保费。当我运行代码时,我希望可以提取156美元的年度保费,但是我得到的只是“年度保费:-”

代码如下:

from selenium import webdriver
import time
from bs4 import BeautifulSoup
import requests

driver = webdriver.Chrome(r"C:\Users\tomwp\Downloads\chromedriver_win32\chromedriver.exe")
page = driver.get("https://www.earthquakeauthority.com/")

xpath = '//*[@id="form"]/header/div[2]/a'
btn = driver.find_element_by_xpath(xpath)
btn.click()

time.sleep(5)

iframe = driver.find_element_by_xpath("//iframe[@id='premiumCalc-iframe']")
driver.switch_to.frame(iframe)

xpath = '//*[@id="cea-page-1"]/div/div/div[1]/div/button[1]'
btn = driver.find_element_by_xpath(xpath)
btn.click()

xpath = '//*[@id="startdate"]'
incept_date = driver.find_element_by_xpath(xpath)
incept_date.send_keys("03/24/2019")

xpath = '//*[@id="participatingInsurer"]'
insurance_company = driver.find_element_by_xpath(xpath)
insurance_company.send_keys("Other")

xpath = '//*[@id="street"]'
street_address = driver.find_element_by_xpath(xpath)
street_address.send_keys("26 Los Indios")

xpath = '//*[@id="zipcode"]'
zip_code = driver.find_element_by_xpath(xpath)
zip_code.send_keys("92618")

xpath = '//*[@id="form-views"]/div[18]/div/button'
btn = driver.find_element_by_xpath(xpath)
btn.click()

xpath = '//*[@id="yearbuilt"]'
year_built = driver.find_element_by_xpath(xpath)
year_built.send_keys("2011")

xpath = '//*[@id="insuredvalue"]'
year_built = driver.find_element_by_xpath(xpath)
year_built.send_keys("100000")

xpath = '//*[@id="numberOfStories"]'
number_stories = driver.find_element_by_xpath(xpath)
number_stories.send_keys("Greater than one")

xpath = '//*[@id="foundationtype"]'
foundation = driver.find_element_by_xpath(xpath)
foundation.send_keys("slab")

xpath = '//*[@id="form-views"]/div[14]/div/button'
btn = driver.find_element_by_xpath(xpath)
btn.click()

soup = BeautifulSoup(driver.page_source, 'lxml')
premium = soup.find('div', class_='gauge-subtitle ng-binding ng-scope')
print(premium.text)

这是我想提取的$ 156:

<div ng-if="isQuoting == false" class="gauge-subtitle ng-binding ng-scope">Annual Premium: $156.00</div>

请注意,iframe ID如下(不确定是否有帮助)

<iframe id="premiumCalc-iframe" style="width: 100%; border: none; height: 1397px;" scrolling="no" src="//calc.earthquakeauthority.com/app/index.html" cd_frame_id_="d0b3a5bcdcfe60ced66a29d282ad86c6"></iframe>

enter image description here

2 个答案:

答案 0 :(得分:1)

我试图通过添加等待条件来使其更加健壮。最终报价页面在最后一次单击时刷新,因此您可能会遇到过时的元素异常。如果您发现刷新完成的体面指标,则应替换当前的time.sleep

我个人会在整个过程中使用CSS选择器,但我坚持使用xpath来与您的代码保持一致。

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(r"C:\Users\tomwp\Downloads\chromedriver_win32\chromedriver.exe")
page = driver.get("https://www.earthquakeauthority.com/")

xpath = '//*[@id="form"]/header/div[2]/a'
btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath)))
btn.click()

iframe = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//iframe[@id='premiumCalc-iframe']")))
driver.switch_to.frame(iframe)

xpath = '//*[@id="cea-page-1"]/div/div/div[1]/div/button[1]'
btn = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, xpath)))
btn.click()


xpath = '//*[@id="startdate"]'
incept_date = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, xpath)))
incept_date.send_keys("03/24/2019")

xpath = '//*[@id="participatingInsurer"]'
insurance_company = driver.find_element_by_xpath(xpath)
insurance_company.send_keys("Other")

xpath = '//*[@id="street"]'
street_address = driver.find_element_by_xpath(xpath)
street_address.send_keys("26 Los Indios")

xpath = '//*[@id="zipcode"]'
zip_code = driver.find_element_by_xpath(xpath)
zip_code.send_keys("92618")

xpath = '//*[@id="form-views"]/div[18]/div/button'
btn = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, xpath)))
btn.click()


xpath = '//*[@id="yearbuilt"]'
year_built = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, xpath)))
year_built.send_keys("2011")

xpath = '//*[@id="insuredvalue"]'
year_built = driver.find_element_by_xpath(xpath)
year_built.send_keys("100000")

xpath = '//*[@id="numberOfStories"]'
number_stories = driver.find_element_by_xpath(xpath)
number_stories.send_keys("Greater than one")

xpath = '//*[@id="foundationtype"]'
foundation = driver.find_element_by_xpath(xpath)
foundation.send_keys("slab")

xpath = '//*[@id="form-views"]/div[14]/div/button'
btn = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, xpath)))
btn.click()

time.sleep(2)

quote = driver.find_element_by_css_selector(".gauge-subtitle").text

print(quote)

答案 1 :(得分:0)

如果我答对了,您就可以导航到估计页面,并且可以看到估计的年度保费价值。

如果是这种情况,请尝试以下代码:

iframe = driver.find_element_by_xpath("//iframe[@id='premiumCalc-iframe']")
yourResult = driver.find_element_by_class_name("gauge-subtitle ng-binding ng-scope").text