【问题标题】:Locate search box but could not enter value with Selenium找到搜索框但无法使用 Selenium 输入值
【发布时间】:2023-01-08 02:15:55
【问题描述】:

我有以下 HTML 结构,我正在尝试使用 Selenium 在搜索框中输入文本。 我clould定位搜索框无法输入文本。这是我写的HTML结构和代码:

<input tabindex="0" data-testid="search-input" data-analytics-link="GlobalNav|Txt|||||Search Bar" class="css-dz6luo" value="">

enter image description here

driver.find_element(By.XPATH, "//input[@data-testid='search-input']/parent::label").send_keys(text)

请帮助我了解这里出了什么问题。

它可以找到元素但无法输入文本。

【问题讨论】:

  • 应该更新问题以包括重现问题所需的最短代码。

标签: python selenium web-scraping


【解决方案1】:

您正在尝试将文本输入到输入元素的父元素(标签元素)中,而不是输入元素本身。

要在输入元素中输入文本,您应该直接在输入元素上使用 .send_keys() 方法。

driver.find_element(By.XPATH, "//input[@data-testid='search-input']").send_keys(text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2018-10-07
    • 2020-03-31
    • 2018-07-24
    • 1970-01-01
    • 2020-12-04
    • 2021-10-22
    相关资源
    最近更新 更多