【问题标题】:Typing text into a search box when using selenium and beautiful soup in Python在 Python 中使用 selenium 和美丽的汤时在搜索框中键入文本
【发布时间】:2021-07-10 19:28:04
【问题描述】:

通常下面的代码可以在搜索栏中输入信息,但是,它不允许我在这个实例上使用 .send_keys()。我需要做什么?我注意到的一件事是,当我实际单击该框时, class="hidden appended" 行在行尾添加了一个灰色的“flex”按钮。不确定这意味着什么,或者是否是以下代码不起作用的原因。

   typetextfirst = driver.find_element_by_id("searchRegistry-text")
   typetextfirst.clear()
   typetextfirst.send_keys(row["Name"])

【问题讨论】:

  • 请分享该网页的链接。我们需要查看输入元素及其行为,
  • 所以你又要了help,最后把efforts都像垃圾一样扔掉了。
  • 对不起,我没有看到你的帖子。我删除了它,因为我收到的答案非常简单,而且我不知道如何关闭答案。如何取消删除?

标签: python selenium web-scraping beautifulsoup sendkeys


【解决方案1】:

网站中存在一些具有相同id 的重复元素。可能这就是造成问题的原因。我试图复制您的场景并编写了以下代码。如果它也适合你,请告诉我。

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")

driver = webdriver.Chrome(options=options)
driver.get('https://www.bedbathandbeyond.com/store/giftregistry/registry-search-guest?icid=static_st_2acr1_'
           'weddingregistry_find_24547')

wait = WebDriverWait(driver, 30)
action = ActionChains(driver)

# Press Cancel to Close the initial Popup
try:
    wait.until(EC.visibility_of_element_located((By.XPATH, '(//a[contains(@id,"bx-close-inside-")])[1]'))).click()
except:
    pass

# Perform Search
searchTextbox = wait.until(EC.presence_of_element_located((By.XPATH, '(//input[@id="searchRegistry-text"])[2]')))
action.move_to_element(searchTextbox).click().send_keys("Hello").perform()

# Click on Search Button
save_Button = wait.until(EC.presence_of_element_located((By.XPATH, '(//button[text()="Search"])[2]')))
action.move_to_element(save_Button).click()

# Search and Click Type Filter
TypeFilter = wait.until(
    EC.presence_of_element_located((By.XPATH, '//button[@data-locator="search-results-type-filter"]')))
TypeFilter.click()

# Click on Wedding Option
WeddingOption = wait.until(
    EC.presence_of_element_located((By.XPATH, '//button[text()="Wedding"]')))
WeddingOption.click()

# Search and Click Year Filter
YearFilter = wait.until(
    EC.presence_of_element_located((By.XPATH, '//button[@data-locator="search-results-year-filter"]')))
YearFilter.click()

# Select 2021 Filter
Option_2021 = wait.until(
    EC.presence_of_element_located((By.XPATH, '//button[text()="2021"]')))
Option_2021.click()

希望它能解决您的问题。谢谢。

【讨论】:

  • +1 以获得出色的诊断。对我来说,至少解决方案可以简化为 wait.until(EC.element_to_be_clickable((By.ID, "searchInput"))) driver.find_elements_by_name('q')[1].send_keys("hello")
  • 所以我遇到了另一个挑战。在下一页上,我希望筛选类型 = 婚礼和年份 = 2021。知道我该怎么做吗?只有当我将鼠标悬停在框上时才会出现下拉菜单,但不要单击。然后我必须点击选定的下拉菜单。
  • 我将编辑我的 Ans 以包括“类型”和“年份”过滤器。谢谢。
【解决方案2】:

尝试在发送文本之前单击该元素,如下所示:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

typetextfirst = wait.until(EC.element_to_be_clickable((By.ID, "searchRegistry-text")))
typetextfirst.click()
typetextfirst.send_keys(row["Name"])

如果这还不够,请尝试发送带有操作的文本:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

typetextfirst = wait.until(EC.element_to_be_clickable((By.ID, "searchRegistry-text")))

actions.move_to_element(typetextfirst)
actions.click()
actions.send_keys(row["Name"])
actions.perform()

【讨论】:

  • 嗯,都没有用。它在最后一行出现错误 actions.perform(): ElementNotInteractableException: Message: element not interactable: [object HTMLInputElement] has no size and location (Session info: chrome=91.0.4472.124)
  • 我认为等待元素可点击性应该可以解决这个问题。查看更新的答案
  • 至少对我来说,等待可点击性超时。另外,当我尝试在较低级别执行操作时,我发现它无法滚动到视图中(这很奇怪,因为它在视图中)
  • 我明白了。而不是等待尝试将 time.sleep 放置几秒钟。这不是一个好方法,但我希望它会有所帮助。让我知道这有帮助
  • 没有这样的运气。我在每个“动作”之间做了 time.sleep(5)。线。显示“(例如 Jane Doe)”的字段甚至没有填写任何文本,并且仍然在 actions.perform() 行上收到相同的错误
猜你喜欢
  • 2019-07-17
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
  • 2018-03-12
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多