Selenium用于驱动程序。通过Internet Explorer的\u name()查找\u元素

2024-05-23 17:44:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在接受在Internet Explorer中自动填写表格的培训。我为Chrome做了以下代码,我成功了,但同样的代码在InternetExplorer上失败了。我的代码是:

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 = "example@gmail.com"
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打开时,它得到了我想要的网站,但在控制台中出现下一个错误时停止:

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

我试过了。通过id查找元素,我也遇到了同样的问题。我不知道为什么,因为这种方法对Chrome非常有效。 有人能帮忙吗


Tags: nameinpyinputbydriverseleniumline
1条回答
网友
1楼 · 发布于 2024-05-23 17:44:29

在访问这些元素之前,必须添加等待/延迟。
请试试这个:

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 = "example@gmail.com"
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()

如果By.NAME不起作用,请按以下方式尝试使用.CSS_选择器:

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 = "example@gmail.com"
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()

相关问题 更多 >