10

I'm trying to enter this site to retrieve my bank account, first I tried with selenium, but only filled username (maybe because it has 2 forms):

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.santandertotta.pt/pt_PT/Particulares.html")
user = driver.find_element_by_name("usr")
user.send_keys("user")
pas = driver.find_element_by_name("claveConsultiva")
pas.send_keys("password")
login = driver.find_element_by_id("login_button").click()

Then, I gone rambo mode :) trying figured out why I can't fill password space, and what are the hidden values of the form using requests, this is the code:

url = "https://www.particulares.santandertotta.pt/pagina/indice/0,,276_1_2,00.html"     
user_agent = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/..."}
session = requests.session()
r = session.get(url)
soup = BeautifulSoup(r.text, "html.parser")    
data = {t['name']:t.get('value') for t in soup.find_all('input', attrs={'type': 'hidden'})}
print(data)    

But just received an empty dict. What is the best approach for enter a site with login and scrape?

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
Sátiro
  • 139
  • 1
  • 1
  • 9

2 Answers2

8

You cannot get access to Password field because it's not present on main page. To handle Password field you have to click Login button to get to Login page. Also you need to switch to iframe which contains authentication form

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.santandertotta.pt/pt_PT/Particulares.html")
driver.find_element_by_xpath("//input[@title='Login de Particulares']").click()
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("ws"))
user = driver.find_element_by_name("identificacionUsuario")
user.send_keys("user")
pas = driver.find_element_by_name("claveConsultiva")
pas.send_keys("password")
pas.submit()
Andersson
  • 51,635
  • 17
  • 77
  • 129
4

Once you access the url https://www.santandertotta.pt/pt_PT/Particulares.html first you have to click on the element with text as Login then only the the Nome and Password field appears but to access those fileds you have to switch to the frame with id as ws inducing WebDriverWait. Next to locate the element of Nome you have to induce WebDriverWait again as follows :

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.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.santandertotta.pt/pt_PT/Particulares.html")
driver.find_element_by_xpath("//input[@class='ttAH_button03']").click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ws")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='inputlong' and @id='identificacionUsuario']"))).send_keys("your_name")
driver.find_element_by_xpath("//input[@id='claveConsultiva' and @name='claveConsultiva']").send_keys("your_password")
driver.find_element_by_link_text("Entrar no NetBanco Particulares").click()

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey, dude, what is the point of making my answer a bit complicated? :) – Andersson Apr 16 '18 at 07:25
  • thank you very much to both of you, that worked... if i want do the same with request how far off am i? – Sátiro Apr 16 '18 at 21:55
  • @Sátiro I am afraid, for the time being I would avoid the context of request. Feel free to ask me any question with Selenium. – undetected Selenium Apr 17 '18 at 02:23
  • hi again, how can i close the java popup in [this](https://www.eurobic.pt/) site, i use wait(driver,2).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='eurobicnet-name']"))).send_keys("bah" , Keys.RETURN) a popup appear – Sátiro Apr 18 '18 at 22:33
  • 1
    @Sátiro Can you please raise a new question with your new requirement. So volunteers will be glad to help you out. – undetected Selenium Apr 20 '18 at 16:57
  • i would post if selenium haven't stoped work, only appear the tab name of the site and a blank page, when i figured this out i post...spend all day trying to figure out happened – Sátiro Apr 20 '18 at 22:26