【问题标题】:How to find search area using selenium in python如何在 python 中使用 selenium 查找搜索区域
【发布时间】:2021-12-30 14:12:49
【问题描述】:

我有这个 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 有没有其他方法比如使用占位符描述?

【问题讨论】:

    标签: python-3.x selenium xpath css-selectors webdriverwait


    【解决方案1】:

    理想情况下,要定位 clickable 元素而不是 presence_of_element_located(),您需要为 element_to_be_clickable() 引入 WebDriverWait,您可以使用以下任一 @987654324 @:

    • 使用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
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2018-11-14
      • 1970-01-01
      • 2018-10-07
      相关资源
      最近更新 更多