驱动程序.send_keys(“Selenium”)属性错误:“WebDriver”对象没有属性“send_keys”

w6mmgewl  于 2023-01-13  发布在  其他
关注(0)|答案(2)|浏览(319)

我一直在尝试在谷歌文本框中输入文本。突然出现这个错误。是什么修改/改变 selenium 新?代码:

from selenium.webdriver.common.keys import Keys
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.Chrome(executable_path="C:\\Program Files (x86)\\chromedriver.exe")
driver.get("https://www.google.com/")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located(By.NAME, "gLFyf")
    )
finally:
    driver.send_keys("Selenium")

错误:

File "C:\Users\bhatt\OneDrive\Desktop\Sublime\gotta_master_selenium.py", line 15, in <module>
    driver.send_keys("Selenium")
AttributeError: 'WebDriver' object has no attribute 'send_keys'

我还有一些错误

```EC.presence_of_element_located(By.NAME, "gLFyf")TypeError: presence_of_element_located() takes 1 positional argument but 2 were given

我整个假期都在想办法,还是想不出办法

wwtsj6pe

wwtsj6pe1#

对于第一个错误:
您必须将send_keys()函数与webelement而不是webdriver一起使用。
语法:

driver.find_element(<locator type like By.XPATH or By.ID or...>, <locator value>).send_keys("<value to enter>")

对于第二个错误:
您必须在下面的行中再使用一个括号'()':

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "gLFyf")))
kt06eoxx

kt06eoxx2#

不能将文本send_keys("Selenium")发送到Web驱动程序对象driver
文本应发送到需要使用driver定位的特定web元素

相关问题