凑和一下硒形式(scrape and clicking form with selenium)

2019-09-28 19:09发布

我想放弃模拟“请求您的在线贷款”这个网站的形式:

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

我的预期输出是当您单击窗体上的结果。 我想找到的网页与利率和金额的结果。

预计outpout但是打印时不正确的是:

其结果就是把我带回了会议:

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

Answer 1:

print(driver)返回的webdriver实例的字符串表示( driver.__str__()和它的正常行为

print(driver.find_element_by_tag_name('body').text)没有返回您提交表单页面后body空的 -它仅包含未显示的页面上,因此剧本text属性返回预期空字符串

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

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']


Answer 2:

以“网上Richiedi金正日陀prestito”杀模拟形式的网站 ,你可以采用如下方案:

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.PhantomJS(executable_path=r'C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
driver.get("https://www.findomestic.it/ ")
driver.maximize_window()
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.XPATH, "//ul[@class='select-list bh-options_list']//li/a[text()='Auto nuova']"))).click()
driver.find_element_by_css_selector("input#findomestic_simulatore_javascript_importo").send_keys("2000")
driver.find_element_by_css_selector("input#findomestic_simulatore_javascript_calcola").submit()


文章来源: scrape and clicking form with selenium