如何找到搜索输入框的正确硒定位器

问题描述 投票:0回答:1

我正在尝试在 swiggy 上自动搜索食物,但当我尝试自动搜索输入框时出现错误。我正在使用 SelectorsHub 来查找定位器,但我仍然面临问题。我在终端中得到以下输出

代码片段:

`search_food = browser.find_element(By.XPATH, "//input[@placeholder='Search for restaurants and food']")
search_food.click()`

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@placeholder='Search for restaurants and food']"}
  (Session info: chrome=123.0.6312.107); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
    GetHandleVerifier [0x00007FF713927032+63090]
    (No symbol) [0x00007FF713892C82]
    (No symbol) [0x00007FF71372EC65]
    (No symbol) [0x00007FF71377499D]
    (No symbol) [0x00007FF713774ADC]
    (No symbol) [0x00007FF7137B5B37]
    (No symbol) [0x00007FF71379701F]
    (No symbol) [0x00007FF7137B3412]
    (No symbol) [0x00007FF713796D83]
    (No symbol) [0x00007FF7137683A8]
    (No symbol) [0x00007FF713769441]
    GetHandleVerifier [0x00007FF713D225AD+4238317]
    GetHandleVerifier [0x00007FF713D5F70D+4488525]
    GetHandleVerifier [0x00007FF713D579EF+4456495]
    GetHandleVerifier [0x00007FF713A00576+953270]
    (No symbol) [0x00007FF71389E54F]
    (No symbol) [0x00007FF713899224]
    (No symbol) [0x00007FF71389935B]
    (No symbol) [0x00007FF713889B94]
    BaseThreadInitThunk [0x00007FFC9DBB257D+29]
    RtlUserThreadStart [0x00007FFC9F24AA48+40]
selenium-webdriver
1个回答
0
投票

检查下面的工作代码:

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

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.swiggy.com/city/bangalore")
wait = WebDriverWait(driver, 10)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Search for restaurant and food']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search for Dishes and Restaurants']"))).send_keys("test")
time.sleep(10)

结果:

© www.soinside.com 2019 - 2024. All rights reserved.