无法使用Selenium webdriver选择并单击下拉搜索查询结果

2024-05-14 16:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从搜索框结果下拉列表中执行选择并单击操作以进行测试。虽然我没有得到蚂蚁的错误,但我卡住了,不能这样做,搜索结果来了,然后立即消失。请任何人帮帮我。我正在使用Python脚本自动化webdriver。以下是供参考的屏幕截图。 enter image description here。我尝试过webdriverwait进行相同的操作,但它给出了超时异常错误。如果CSS中有任何子操作需要执行,请告诉我。这是我试过的

search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")

dropdown = WebDriverWait(driver, 4).until(
        EC.presence_of_element_located((By.XPATH, "//li[text()='flowers']")))

除了这段代码之外,我希望只执行enter键操作,在这个ecomm上获取“flower”的查询结果。网站

以下是网站URL-https://paytmmall.com


Tags: 脚本列表search屏幕网站driver错误element
2条回答

建议的选项没有直接在li元素中包含文本,而是在li元素中的子元素中。
请尝试以下方法:

search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")

dropdown = WebDriverWait(driver, 4).until(
EC.visibility_of_element_located((By.XPATH, "//li//*[text()='flowers']")))

input field中键入flower后,将根据提供的输入显示多个选项。它们位于li tagsb tag之下

代码:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://paytmmall.com/")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.XPATH, "(//li)[4]/descendant::b[contains(text(),'flowers')]"))).click()

time.sleep仅用于可见性目的。你也可以删除它

另外,这个xpath(//li)[4]/descendant::b[contains(text(),'flowers')]基于xpath索引,因为我认为您需要选择第四个选项,即flower本身。如果您想要select一个不同的选项,您必须编写不同的xpath

如果您只想选择搜索的项目,最好在输入字段中键入flower后通过enter键

您可以使用以下代码进行此操作:

search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
search.send_keys(Keys.RETURN)

相关问题 更多 >

    热门问题