如何从相同的输入字段中选择唯一元素

问题描述 投票:-1回答:1

请在下面找到所选输入字段的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;">
java selenium selenium-webdriver xpath css-selectors
1个回答
0
投票

您可以将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中使用reference元素。

© www.soinside.com 2019 - 2024. All rights reserved.