【问题标题】:Can we use the search functionality on the website using selenium web browser我们可以使用 selenium 网络浏览器在网站上使用搜索功能吗
【发布时间】:2019-11-07 09:36:52
【问题描述】:

我正在从事网络抓取项目,我想通过在 Python 中使用 Selenium Web 驱动程序在网站的搜索框中输入关键字来搜索特定元素。

例如这是网站:www.auchan.fr 我想搜索这个 EAN “4020628743819” 号码。

我尝试了以下代码,但它不起作用:

text_area = driver.find_element_by_xpath('//*[@id="search"]/form/input')
text_area.send_keys('4020628743819')
submit_button = driver.find_elements_by_css_selector(".search-submit[type='submit']").submit()

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver web-scraping


    【解决方案1】:

    xpath //*[@id="search"]/form/input 匹配两个元素。搜索栏是第二个,但find_element_by_xpath 返回第一个。您可以通过添加到第一个结果的祖先元素的clone 类来区分并查找没有此类的元素

    text_area = driver.find_element_by_xpath('//header[not(contains(@class, "clone"))]//div[@id="search"]//input')
    

    【讨论】:

    • 现在,submit_button = driver.find_elements_by_css_selector(".search-submit[type='submit']").submit() 这行给了我一个错误:'list' object has no attribute 'submit'
    • @PujaNeve find_elements 返回列表,使用find_element
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2012-05-18
    • 2023-03-10
    相关资源
    最近更新 更多