我无法在Selenium(Python)中使用send_keys。

huangapple go评论57阅读模式
英文:

I can't use send_keys in Selenium (Python)

问题

我试图在Python中进行基本的网络自动化。我正在运行一个测试,并且在运行时遇到错误消息:

search.send_keys("Test")
AttributeError: 'list' object has no attribute 'send_keys'

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

url = "https://www.youtube.com"

browser = webdriver.Chrome()
browser.get(url)
time.sleep(3)

search = browser.find_elements(By.XPATH, "//*[@id='search']")

search.send_keys("Test")

如何修复它?

英文:

I am trying to do basic web automation in Python. I am running a test. And I get an error message when I run everything saying:

search.send_keys("Test")
AttributeError: 'list' object has no attribute 'send_keys'

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

url = "https://www.youtube.com"

browser = webdriver.Chrome()
browser.get(url)
time.sleep(3)

search = browser.find_elements(By.XPATH, "//*[@id='search']")

search.send_keys("Test")

How can ı fix it?

答案1

得分: 0

"browser.find_elements" 获取具有您发送的 XPath 的所有元素,返回一个包含所有元素的数组,然后您可以迭代这些元素。

您需要选择单个对象:

search = browser.find_element(By.XPATH, "//*[@id='search']")
search.send_keys("Test")

如果该代码对您不起作用,请尝试使用类似 xPath Finder 这样的工具来获取您想要操作的元素的唯一 XPath。

英文:

"browser.find_elements" gets all the elements with the xpath you sent, returning an array with all the elements that you can then iterate over.

You need to select an individual object instead:

search = browser.find_element(By.XPATH, "//*[@id='search']")
search.send_keys("Test")

If that code doesn't work for you, try using tools like xPath Finder to grab a unique xPath of the element you want to manipulate.

huangapple
  • 本文由 发表于 2023年2月18日 18:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定