Selenium for driver.find_element_by_name() for Internet Explorer

602 Views Asked by At

I am training to fill forms automatically in Internet Explorer. I did the following code for Chrome and I succeeded but the same code fails for Internet Explorer. My code is:

from selenium import webdriver
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(executable_path="C:/Program Files (x86)/IEDriverServer.exe", capabilities={'ignoreZoomSetting':True})

driver.get("https://rosamorel.com/ebook-copywriting-textos-persuasivos/")
nombre = "example"
email = "[email protected]"
accionar = driver.find_element_by_name("input_1.3")
accionar.send_keys(nombre)
accionar = driver.find_element_by_name("input_2")
accionar.send_keys(email)
accionar = driver.find_element_by_name("input_5.1")
accionar.click()

IE opens, it gets the website I want but it stops there with the next error in the Console:

Traceback (most recent call last):
  File "c:\Users\Abel\Documents\Ejercicios_python\web_scrapin_test copy.py", line 11, in <module>
    accionar = driver.find_element_by_name("input_1.3")
  File "C:\Users\Abel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name     
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\Abel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Abel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Abel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response        
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with name == input_1.3

I tried .find_element_by_id and I have the same problem. I do not why because this method works perfectly for Chrome ¡. Could anyone help?

1

There are 1 best solutions below

2
Prophet On

You have to add a wait / delay before accessing these elements.
Please try this:

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
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(executable_path="C:/Program Files (x86)/IEDriverServer.exe", capabilities={'ignoreZoomSetting':True})
wait = WebDriverWait(driver, 20)

driver.get("https://rosamorel.com/ebook-copywriting-textos-persuasivos/")
nombre = "example"
email = "[email protected]"
wait.until(EC.visibility_of_element_located((By.NAME, "input_1.3"))).send_keys(nombre)

wait.until(EC.visibility_of_element_located((By.NAME, "input_2"))).send_keys(email)

wait.until(EC.visibility_of_element_located((By.NAME, "input_5.1"))).click()

In case By.NAME will not work try By.CSS_SELECTOR as following:

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
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(executable_path="C:/Program Files (x86)/IEDriverServer.exe", capabilities={'ignoreZoomSetting':True})
wait = WebDriverWait(driver, 20)

driver.get("https://rosamorel.com/ebook-copywriting-textos-persuasivos/")
nombre = "example"
email = "[email protected]"
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='input_1.3']"))).send_keys(nombre)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='input_2']"))).send_keys(email)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='input_5.1']"))).click()