英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论