在 Selenium 中使用 xpath 在文本框中输入文本

Enter text within text box using xpath in Selenium

我正在尝试找到一个文本框以在其中输入文本,但由于错误 'no such element: Unable to locate element'

而无法找到

这是页面的HTML:

<form id="aaaSub" name="aaaSub" method="post" action="/ProvGwAaaGui/faces/jsp/dspAAABySearch.jsp" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="aaaSub" value="aaaSub" />
<table id="aaaSub:msg1" style="height:30px; margin-left:20px;">
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table id="aaaSub:heading" style="height:60px; margin-left:20px;">
<tbody>
<tr>
<td><table class="ad_wa_title_bold">
<tbody>
<tr>
<td>Display Single Umc AAA Subscriber</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table class="ad_wa_table" border="1" style="margin-left:20px;">
<tbody>
<tr>
<td><table class="ad_wa_body" style="margin:10px 10px 10px 10px;">
<tbody>
<tr>
<td><table style="width:300px;">
<tbody>
<tr>
<td><span id="aaaSub:j_id_jsp_2095096634_11" class="ad_wa_body">Identifier</span></td>
</tr>
</tbody>
</table>
</td>
<td><input id="aaaSub:identifier" name="aaaSub:identifier" type="text" value="" maxlength="32" size="30" onkeyup="disableOtherLink('aaaSub:identifier')" onchange="disableOtherLink('aaaSub:identifier')" /></td>
</tr>
<tr>
<td><table style="width:300px;">
<tbody>
<tr>
<td><span id="aaaSub:j_id_jsp_2095096634_15" class="ad_wa_body">cn alias</span></td>
</tr>
</tbody>
</table>
</td>
<td><input id="aaaSub:cn" name="aaaSub:cn" type="text" value="" maxlength="32" size="30" onkeyup="disableOtherLink('aaaSub:cn')" onchange="disableOtherLink('aaaSub:cn')" /></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table id="aaaSub:empty" style="height:130px; margin-left:20px;">
<tbody>
<tr>
<td><span id="aaaSub:j_id_jsp_2095096634_18"> </span></td>
</tr>
</tbody>
</table>
<table id="aaaSub:buttons" class="ad_wa_body" style="width:200px;margin-left:20px;">
<tbody>
<tr>
<td><input type="submit" name="aaaSub:j_id_jsp_2095096634_20" value="Display" style="height:25px;margin-left:5px;width:100px" onclick="return(mandatoryGeneralCheck())" /></td>
</tr>
</tbody>
</table>
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAANUaa2wcR3nu/L4kTeI0cV9OnMRgG5KL37HzaLGd17Xnh3JOaFPgMrc38a2zt7udnfPdNSS0RdAKVLUo8AMpFUggfsXlBxJUqLQFfiAhiiAIkEAqLX9KEUEIIUH4Efhm9nmXXe+dHVK6P+Z2Z2e++d6vvavXUVOBoi2PJxfxEo4rWF2Iz2YWicQOvvjzR7+2yehTogiVdITQVoOiIUnLx42CGj+HJWLEsa4rsoSZrKnxFMOMTGMVLxCayOtK9zwlZEbLkn+c+/5ry/3HX1/P4RQPI3518dNKFhSAqWsqUVn8VOK0TIonNY2hrYtpOZteNPT0YP/4SP/46OjQcLq/ZDyBLqFocUBA2e0PJcfySvwEDMc0mkfNGONUIWPvHIZ9EdQbsnMOq0Q5TuUsaswbCwP25iRsjqJBjS4A6VjKkXi+7AuDlJiAM00MAxhioHZSkojOGbU3b83ZQPfXiVFLjuCsrC7Y+4/C/sY69vtydsSGdgygNaG+EGizBaYX2DyQ6Q9u1ItcPcT5QtvvhdayVmhjXmita4U2bkM7BNDa0PYgxZ7SlEJeRdv8YAw4en0SgMTQcO0K5hGEP+iBSmqHagedUG3IMTkLb+VzMqFeNamHd/7IDXmZt2GVzBv2Mu+u28o8xyoOr555UUm1oYzUaQxNJK+zspe8zbeVvLFVu6BMgTFNdVzYw7B/C9oTsn9Ky+exmp0Ue/1RGuwvFThIfkV97pp1uJynjZco2hsWSbrnqKYTysqPkLKBrKsdYhFFd7kx76hayHtf6gw1K5qEFQJRb5NYV2CyEk+KuUv/2nz2Sv+NvzSg5gRqzWEjJ0GgS6IWSSuojJYZahfxdB+HvS/FKLjrg0kUA1EQ1YAgIPi2M4la+YICRAPrudmQqKwz66llCVMZq+ZjS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9+LmUbLx4lKKFamckQ639vXJxSEoXbv6eYZgFPAp1/xzYkDDENjE5Z4Iu+m7VBU9AY1uqYJy2lZd+1D6y7g33TQ581O171OC6t63RPJjnVnXnjnb1Hr3DdWamY5snP8vhkpwVcf0+gRR4oU7apsZlWfysufxA38e2Xg0zut8ifyXbObtSLoWxWg+n9a1nfl5Wun332v88JxuzRDXFGEX1p2v0D0WB5xcKTKaPizaVAD3KAqrXPFLlHk2577H3gNxA32LwmP9F9xhFd8ZSYAAA==" autocomplete="off" />
</form>
    </div>
</body>
</html>

请求的框在这里:

<td><input id="aaaSub:identifier" name="aaaSub:identifier" type="text" value="" maxlength="32" size="30" onkeyup="disableOtherLink('aaaSub:identifier')" onchange="disableOtherLink('aaaSub:identifier')" /></td>

我尝试了以下代码

web.implicitly_wait(30)
web.find_element(By.XPATH,'//*[@id="aaaSub:identifier"]').send_keys('text')

您使用的 xpath 定位器无效。 请使用//input[@id="aaaSub:identifier"]

所需的元素是一个动态元素,因此理想情况下发送一个字符序列到你需要诱导的元素WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='aaaSub:identifier'][name='aaaSub:identifier'][onchange*='aaaSub:identifier']"))).send_keys("text")
    
  • 使用 XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='aaaSub:identifier' and @name='aaaSub:identifier'][contains(@onchange, 'aaaSub:identifier')]"))).send_keys("text")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

参考资料

您可以在以下位置找到关于 的一些相关讨论: