如何通过seleniumwebdriver和Python在没有唯一标识符的文本框中单击

2024-06-16 11:26:02 发布

您现在位置: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测试,遇到了一个没有唯一标识符的元素,因为它正在从端点自动获取其值。你知道吗


Tags: namedivfalsetrueinputdatavaluebind
1条回答
网友
1楼 · 发布于 2024-06-16 11:26:02

根据您共享的HTML来定位对应于{userid}字段的textbox,您需要诱导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")
    

相关问题 更多 >