用硒刮擦并单击表格 [英] scrape and clicking form with selenium

查看:70
本文介绍了用硒刮擦并单击表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要取消此网站的模拟 Richiedi il tuo prestito online:

I want to scrap the simulation "Richiedi il tuo prestito online" of a form of this website:

https://www.findomestic.it/ 

我尝试过:

driver = webdriver.PhantomJS()
driver.get("https://www.findomestic.it/")
raison = driver.find_element_by_xpath("//a[@href='javascript:void(0);']")
montant = driver.find_element_by_id('findomestic_simulatore_javascript_importo')
submitButton = driver.find_element_by_id('findomestic_simulatore_javascript_calcola')
actions = ActionChains(driver).click(raison).send_keys('AUTO NUOVA').click(montant).send_keys('2000').send_keys(Keys.RETURN)
actions.perform()
print(driver.find_element_by_tag_name('body').text)
print(driver)
driver.close()

我的预期输出是单击表单时的结果。我想找到包含利率和金额的网页结果。

My expected output is the result when you click on the form. I want to find the results of the web page with the interest rate and the amount.

预期支出
但打印不正确:

expected outpout But the print is not correct:

结果只是让我退回会话:

The result is just sends me back the session:

<selenium.webdriver.phantomjs.webdriver.WebDriver(session="c4070330-18b2-11e9-81cf-2dbe9dae6b83")>


推荐答案

print(driver)返回WebDriver实例的字符串表示形式( driver .__ str __()),这是正常行为

print(driver) returns the string representation of WebDriver instance (driver.__str__()) and it's normal behavior

print(driver.find_element_by_tag_name('body')。text)不返回任何内容,就像您提交表单页面 body -它只包含未在页面上显示的脚本,因此 text 属性将按预期返回空字符串

print(driver.find_element_by_tag_name('body').text) returns nothing as after you submit the form page body is empty - it contains only scripts that are not displayed on page and so text property returns empty string as expected

您需要等待结果显示在页面上:

You need to wait for results to appear on page:

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 import webdriver

driver = webdriver.PhantomJS()
driver.get("https://www.findomestic.it/")

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.select.bh-option"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'AUTO NUOVA'))).click()
driver.find_element_by_id("findomestic_simulatore_javascript_importo").send_keys("2000")
driver.find_element_by_id('findomestic_simulatore_javascript_calcola').click()

for item in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'ul.fd-siff-element > li')))[1:]:
    print(item.text.split('\n')[:-1])

输出应为

['56,20 € PER', '42 MESI', '9,54 % TAN FISSO', '9,97 % TAEG FISSO']
['64,10 € PER', '36 MESI', '9,53 % TAN FISSO', '9,96 % TAEG FISSO']
['75,20 € PER', '30 MESI', '9,54 % TAN FISSO', '9,97 % TAEG FISSO']
['91,80 € PER', '24 MESI', '9,46 % TAN FISSO', '9,89 % TAEG FISSO']
['119,70 € PER', '18 MESI', '9,54 % TAN FISSO', '9,97 % TAEG FISSO']

这篇关于用硒刮擦并单击表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆