如何在 python 中使用 selenium 查找搜索区域

How to find search area using selenium in python

我有这个html:

<input _wad-f341 type="text" autocomplete="off" kkwdnkwn class="classname" placeholder="Search  here" id="ppui-search-0-input">

使用开发人员工具,我正在获取 xpath,我正在使用以下代码检查搜索区域是否已加载。

WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//*[@id='ppui-search-0-input']")))

我正在超时,因为找不到元素。谁能帮帮我吗? 除了使用占位符描述之类的 id 之外,还有其他方法吗?

理想情况下定位 可点击 元素而不是 you need to induce WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.classname[placeholder='Search  here'][id^='ppui-search']")))
    
  • 使用XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='classname' and @placeholder='Search  here'][starts-with(@id, 'ppui-search')]")))
    
  • 注意:您必须添加以下导入:

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