首页 > 解决方案 > 如何通过 selenium-webdriver 和 Python 根据 html 在没有唯一标识符的文本框中单击

问题描述

<div class="panel">
      <div data-bind="foreach: UriParameters">
        <div>
          <input readonly="true" spellcheck="false" tabindex="100" class="uriParameterLabel" data-bind="value: '{' + name + '}'">
          <span>= </span>
          <input spellcheck="false" data-bind="value: value, valueUpdate: 'afterkeydown', enable: enabled">
          <input type="checkbox" data-bind="checked: enabled">
        </div>
      
        <div>
          <input readonly="true" spellcheck="false" tabindex="100" class="uriParameterLabel" data-bind="value: '{' + name + '}'">
          <span>= </span>
          <input spellcheck="false" data-bind="value: value, valueUpdate: 'afterkeydown', enable: enabled">
          <input type="checkbox" data-bind="checked: enabled">
        </div>
      </div>
    </div>

我正在学习自动化 API 测试并遇到一个没有唯一标识符的元素,因为它会自动从端点获取其值。

标签: pythonseleniumselenium-webdriverwebdriverwebdriverwait

解决方案


根据您共享的HTML ,以找到与您需要诱导WebDriverWait元素可点击的字段对应的文本框,您可以使用以下解决方案:{userid}

  • XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='panel']/div[contains(@data-bind, 'UriParameters')]/div//following::input[2]"))).click()
    #or
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='panel']/div[contains(@data-bind, 'UriParameters')]/div//following::input[2]"))).send_keys("Ice")
    

推荐阅读