python硒选择正确的输入字段

发布于 2025-02-11 17:50:44 字数 1344 浏览 0 评论 0原文

我正在尝试选择正确的输入字段来为其添加一个值。我当前的代码部分有效,它仅选择到第一个输入字段,但我希望能够选择要选择的字段并添加 这是我正在根据值[peterave,sterling等...]选择我的输入字段的Web结构

<tr role="row" class="tester odd">
  <td class="hidden"></td>
  <td class="hidden">222</td>
  <td class="rester-table-cell">
    <input type="hidden" name="building[0].model" value="PeterAVE">
    <input type="text" name="building[0].size" class="control-input-num">
  </td>
  <td class="soreted_1" data-search="PeterAVE" data-order="PeterAVE">    
    <a href="/resilisting/p/PeterAVE">PeterAVE</a> 
  </td>
</tr>
<tr role="row" class="tester even">
  <td class="hidden"></td>
  <td class="hidden">333</td>
  <td class="rester-table-cell">
    <input type="hidden" name="building[1].model" value="Sterling">
    <input type="text" name="building[1].size" class="control-input-num">
  </td>
  <td class="sorted_1" data-search="Sterling" data-order="Sterling">
    <a href="/resilisting/p/Sterling">Sterling</a>
  </td>
</tr>

,我的代码仅选择并填充Peterave的输入字段,因为这是第一个,并且有数百个我希望能够选择。

这是我使用的代码。

driver.find_element_by_xpath("//input[contains(@class,'control-input-num')] ").send_keys("1")

我非常感谢任何帮助。

I am trying to select the correct input field to add a value to it. My current code partially works and it only selects to the first input field but I want to be able to manage which field I want to select and add
here is what the web structure looks like

<tr role="row" class="tester odd">
  <td class="hidden"></td>
  <td class="hidden">222</td>
  <td class="rester-table-cell">
    <input type="hidden" name="building[0].model" value="PeterAVE">
    <input type="text" name="building[0].size" class="control-input-num">
  </td>
  <td class="soreted_1" data-search="PeterAVE" data-order="PeterAVE">    
    <a href="/resilisting/p/PeterAVE">PeterAVE</a> 
  </td>
</tr>
<tr role="row" class="tester even">
  <td class="hidden"></td>
  <td class="hidden">333</td>
  <td class="rester-table-cell">
    <input type="hidden" name="building[1].model" value="Sterling">
    <input type="text" name="building[1].size" class="control-input-num">
  </td>
  <td class="sorted_1" data-search="Sterling" data-order="Sterling">
    <a href="/resilisting/p/Sterling">Sterling</a>
  </td>
</tr>

I am selecting my input field based on values [PeterAVE,Sterling, etc...] currently my code is only selecting and filling the input field for PeterAVE since it's the first one and there are hundreds that I want to be able to choose.

Here is the code I am using.

driver.find_element_by_xpath("//input[contains(@class,'control-input-num')] ").send_keys("1")

I really appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘑菇王子 2025-02-18 17:50:44

您可以在索引中找到所有输入字段

input_fields = driver.find_elements_by_class_name('control-input-num')

,然后选择“索引要求”。

input_fields[10].send_keys('1')

您还可以通过先前的隐藏输入的值选择输入字段:

driver.find_element_by_xpath("//input[@value='Sterling']/following-sibling::input[@class='control-input-num']").send_keys("1") 

You can find all the input fields with

input_fields = driver.find_elements_by_class_name('control-input-num')

and then select required by index

input_fields[10].send_keys('1')

You can also select input field by the value of preceding hidden input:

driver.find_element_by_xpath("//input[@value='Sterling']/following-sibling::input[@class='control-input-num']").send_keys("1") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文