【问题标题】:python selenium interact with input of type searchpython selenium 与搜索类型的输入交互
【发布时间】:2021-09-01 05:37:59
【问题描述】:

这是一个搜索页面。它的结构是这样的。

<input type="search" title="Search Site" aria-label="Keywords" placeholder="Keywords" autocomplete="off" value="">

<div class="header2020-search-button">
    <button class="fast fat-search">
        <span class="hid-text">Search</span>
    </button>
</div>

我正在尝试使用 selenium 与此文本框交互并搜索单词 Collapse。我的代码是这样的

driver.find_element_by_xpath("//input[@title='Search Site')]").send_keys("Collapse")
driver.find_element_by_xpath('//button[@class="fast fat-search"]').click()

发送键不起作用。导致错误。

selenium.common.exceptions.InvalidSelectorException:消息:无效 选择器:无法使用 xpath 表达式定位元素 //input[@title='Search Site')] 因为以下错误: SyntaxError:无法对“文档”执行“评估”:字符串 '//input[@title='Search Site')]' 不是有效的 XPath 表达式。

非常感谢。

【问题讨论】:

    标签: python selenium xpath find element


    【解决方案1】:

    看起来像是错字。
    在 HTML 中,元素类名称是“fas fa-search”,而在代码中我看到“fast fat-search”
    我建议您为此使用以下内容:

    driver.find_element_by_xpath('//div[@class="header2020-search-button"]//button[contains(@class,"search")]').click()
    

    【讨论】:

    • 修正了...错字...但那不是错误。
    【解决方案2】:

    好吧,我得到了错误....有时不同的布局有助于看得更清楚。

    xpath 中有一个额外的 )。

    【讨论】:

    • 我明白了,您的错字在//input[@title='Search Site')] XPath 中。虽然我建议的解决方案应该可以工作:)
    • 我认为最好删除这个问题而不是回答你自己的解决方案,这实际上是一个非常基本的事情,又是一个语法错误
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多