微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 中使用 selenium 和美丽的汤时在搜索框中键入文本

如何解决在 Python 中使用 selenium 和美丽的汤时在搜索框中键入文本

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

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

enter image description here

解决方法

网站中存在一些具有相同 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()

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

,

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

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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。