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

时间:2018-09-07 07:01:02

标签: python selenium selenium-webdriver webdriver webdriverwait

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

1 个答案:

答案 0 :(得分:0)

根据您共享的 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")