【问题标题】:How Select unique element from same inputs fields如何从相同的输入字段中选择唯一元素
【发布时间】:2018-09-08 10:00:31
【问题描述】:

请在下面找到两个选定输入字段的 HTML,但我需要选择第一个,请在我的 XPath 下方找到 - 请帮助我提前谢谢

//*[contains(@id,'CC_CARD_NUMBER_!SEPARATOR') and 
    contains(@parentquestion,"|_VISA")]

第一个输入

<input value="" class="form-control flleft fln masked" defaultvalue="" 
 id="CC_CARD_NUMBER_!SEPARATOR!_9beb0a0e-e089-4d9b-a7c3-75e69f40484c" 
 onclick="showChildQuestions(this);" 
 parentquestion="CC_CARD_TYPE_!SEPARATOR!_4185c1bd-b262-431e-a05d-c174f926a8bf_|_VISA" 
 questionid="CC_CARD_NUMBER_!SEPARATOR!_9beb0a0e-e089-4d9b-a7c3-75e69f40484c" 
 type="text" autocomplete="off" maxlength="16" 
 style="-webkit-text-security: disc;">

第二次输入

<input value="" class="form-control flleft fln masked" defaultvalue="" 
 id="CC_CARD_NUMBER_!SEPARATOR!_3c59ab5f-a770-454a-8ee6-d76fc32a9dea" 
 onclick="showChildQuestions(this);" 
 parentquestion="CC_CARD_TYPE_!SEPARATOR!_e245a7f5-d623-4a79-ad42-e2ec6c8c0dbf_|_VISA" 
 questionid="CC_CARD_NUMBER_!SEPARATOR!_3c59ab5f-a770-454a-8ee6-d76fc32a9dea" 
 type="text" autocomplete="off" maxlength="16" 
 style="-webkit-text-security: disc;">

【问题讨论】:

  • &lt;input&gt; 标签相同,要构建唯一定位器,您需要使用父节点更新问题。

标签: java selenium selenium-webdriver xpath css-selectors


【解决方案1】:

您可以将 xpath 与索引一起使用:

(//*[contains(@id,'CC_CARD_NUMBER_!SEPARATOR') and contains(@parentquestion,"|_VISA")])[1]
(//*[contains(@id,'CC_CARD_NUMBER_!SEPARATOR') and contains(@parentquestion,"|_VISA")])[2]

或者你可以用 Selenium 来做:

List<WebElement> inputs = driver.findElements(By.xpath("//*[contains(@id,'CC_CARD_NUMBER_!SEPARATOR') and contains(@parentquestion,"|_VISA")]"));
inputs.get(0).click();
inputs.get(1).click();

或者你可以在html中使用引用元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多