小能豆

Selenium/Python doesn't recognize the function send_keys, it shows like plain text

py

This is the code I’m running

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains



options = Options()
options.add_argument("start-maximized")

#Don't close the nav
options = Options()
options.add_experimental_option("detach", True)

# Chrome is controlled by automated test software
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
s = Service(r'C:\Users\santi\Downloads\a\chromedriver-win64\chromedriver-win64\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

# Selenium Stealth settings
stealth(driver,
      languages=["en-US", "en"],
      vendor="Google Inc.",
      platform="Win32",
      webgl_vendor="Intel Inc.",
      renderer="Intel Iris OpenGL Engine",
      fix_hairline=True,
  )

driver.get("https://ikariam.es")
driver.implicitly_wait(10)

#Email Fill In
driver.find_element(By.XPATH, '//*[@id="loginRegisterTabs"]/ul/li[1]').click();
mailfinder = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div[2]/div/input').click()
driver.implicitly_wait(10)
mailfinder = driver.send_keys('mail@outlook.com');

Note: I change the email for security measures.

DevTools listening on ws://127.0.0.1:51575/devtools/browser/29423f16-f79c-4613-8156-4ed757ed937a Traceback (most recent call last): File “c:\Users\santi\OneDrive\Escritorio\ikabot2.py”, line 43, in mailfinder = driver.send_keys('mail@outlook.com’); ^^^^^^^^^^^^^^^^ AttributeError: ‘WebDriver’ object has no attribute ‘send_keys’ PS C:\Users\santi>

I don’t know how to fix it. I did uninstall selenium and installed it back, it didn’t work.

I’m trying to use the send_keys() function of webdriver on Chrome for filling up a form for automated login on this and other websites.

[enter image description here][1]

This is what appears over the send_keys() function [1]: https://i.stack.imgur.com/T8Dar.png


阅读 62

收藏
2023-12-23

共1个答案

小能豆

The error is occurring because you are trying to use the send_keys method on the webdriver.Chrome object itself, which doesn’t have a send_keys method. Instead, you should use it on an element, like an input field.

In your code, after clicking on the login tab, you are trying to use send_keys on the webdriver.Chrome object:

mailfinder = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div[2]/div/input').click()
driver.implicitly_wait(10)
mailfinder = driver.send_keys('mail@outlook.com')

You should change it to:

mailfinder = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div[2]/div/input')
mailfinder.click()  # Click to focus on the input field
mailfinder.send_keys('mail@outlook.com')

This way, you find the input field using find_element, click on it to focus, and then use send_keys on the element to input the email address.

Make sure to remove the unnecessary semicolons at the end of lines, as they are not required in Python. Also, avoid assigning the result of click() to the variable if not needed. The corrected code would be:

mailfinder = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div[2]/div/input')
mailfinder.click()  # Click to focus on the input field
mailfinder.send_keys('mail@outlook.com')
2023-12-23