如何根据 html 通过 selenium-webdriver 和 Python 在没有唯一标识符的文本框中单击

How to click within the textbox with no unique identifier as per the html through selenium-webdriver and Python

<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 测试自动化,遇到一个没有唯一标识符的元素,因为它会自动从端点获取其值。

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

  • 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")