【问题标题】:Why function 'send_keys' from selenium doesn't work?为什么 selenium 中的函数“send_keys”不起作用?
【发布时间】:2022-06-30 23:52:57
【问题描述】:

我是 Selenium 的新手,我正在尝试创建我的第一个自动化测试,其中操作系统将打开 Chrome 浏览器,打开 YouTube 并在搜索栏中输入一个词。 好吧,浏览器打开,YouTube 打开,但操作系统没有输入任何单词。 这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//*[@id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

【问题讨论】:

标签: python selenium


【解决方案1】:

页面中似乎可能有 2 个元素的 id 与 search 相同,我们需要获取具有 id 搜索的输入元素。

下面的代码,现在应该可以正常工作了。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[@id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2022-12-18
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多