Python Selenium Message: Unable to locate element

0

I am developing a web scraper in python that what it does is to take users and passwords from a database and then go to an external web page and fill out the form to later log in, it works perfect with the first username and password, but when it happens to the second it throws me that the "submit" button can not be located.

Any ideas?

I leave the code that I am using:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import MySQLdb

db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="seliso")

cursor = db.cursor()

cursor.execute("SELECT * FROM usuarios")

for row in cursor.fetchall():
    rfc = row[1]
    clave = row[2]

    driver = webdriver.Firefox()
    driver.get("https://login.siat.sat.gob.mx/nidp/idff/sso?id=mat-ptsc-totp&sid=10&option=credential&sid=10")

    username = driver.find_element_by_name("Ecom_User_ID")
    password = driver.find_element_by_name("Ecom_Password")

    username.send_keys(rfc)
    password.send_keys(clave)

    submit = driver.find_element_by_name("submit")

    submit.click()

    driver.get("https://www.siat.sat.gob.mx/PTSC/")

    link = driver.find_element_by_link_text("Buzón tributario")
    link.click()

    driver.get("https://www.siat.sat.gob.mx/PTSC/cerrarSesion")

    driver.close()

db.close()
    
asked by Gustavo Serna 12.07.2016 в 21:34
source

1 answer

1

I have often encountered the problem you describe. The reason why you do not see the element in case you are not identifying it wrong, is that you have not given time to load the entire site and build the html from the code that defines it.   There are many efficient solutions, including classes from the library of selenium but in my particular case I usually resolve the issue waiting one or two seconds after the page is loaded

import time

# Todo tu código...
driver.get('...')

# Haces tiempo para que se forme el contenido del html y sea "visible"
time.sleep(2)

# Continúas con tu código
username = driver.find_element...

I hope I have helped. Greetings!

    
answered by 25.01.2017 в 16:05