简体   繁体   English

使用Python和Selenium Webdriver,根据文本值从运行时生成的复选框列表中选择一个复选框

[英]Select a check box from a list of run time generated check boxes on the basis of a text value, using Python and Selenium Webdriver

<tr class="tinside">
<td style="width:80px;">
    <input type="checkbox" onclick="checkCheck(4);" value="1151218123182|N" 
    name="cityno">
    <input type="hidden" value="0" name="dds_id4">
    DELHI 
</td>

I can select checkbox by using below command driver.find_element_by_xpath("//input[@type='checkbox' and @onclick='checkCheck(5);']").click() 我可以使用下面的命令driver.find_element_by_xpath("//input[@type='checkbox' and @onclick='checkCheck(5);']").click()选择复选框

but first we have to know the numeric value like 5 used in the command. 但是首先我们必须知道命令中使用的数字值(如5)。 numeric values are assigning at run time like delhi -- 4 mumbai --5 jaipur --6 London --7 paris -- 8 Lucknow --9 在运行时分配数值,例如德里-4孟买-5斋浦尔-6伦敦-7巴黎-8勒克瑙-9

I only knows the city name but values are assigned at run time and hidden ... so input parameter is city name. 我只知道城市名称,但是值是在运行时分配的并且是隐藏的,因此输入参数是城市名称。 Thanks in advance for sharing your valuable suggestion.. 在此先感谢您分享宝贵的建议。

UPDATE #2 更新#2

 <html><head> 
<body>
<div id="container"><div class="align_center">
<table class="table_style">
<tbody><tr class="table_header">
<tr class="tinside">
<td style="width:80px;">
<input type="checkbox" onclick="checkCheck(12);" value="1151228060704|N" name="ordno">
<input type="hidden" value="0" name="dds_id12"><input type="hidden" value="CANS" name="status12">DLEH</td>
<td class="center_align">ABC</td>
<input type="hidden" value="9341" id="stk_token12" name="stk_token12"><td class="center_align">XYZ</td>
<input type="hidden" value="NN" id="stk_me12" name="stk_me12"><td class="center_align">11275</td>
<td class="center_align">-</td>
<td class="center_align">0.00</td>
<td class="center_align">-</td>
<td class="center_align">Item1</td>
<td class="center_align">Price</td>
<td class="center_align">Rs. 30.40</td>
<td class="center_align">Dec 27 2015</td>
<td class="center_align">0.00</td>
<td class="center_align">0 </td>
<td class="center_align">NN</td>
</tr>
<tr class="tinside">
<td style="width:80px;">
<input type="checkbox" onclick="checkCheck(12);" value="1151228060704|N" name="ordno">
<input type="hidden" value="0" name="dds_id12"><input type="hidden" value="CANS" name="status12">DELHI</td>
<td class="center_align">ABC</td>
<input type="hidden" value="9341" id="stk_token12" name="stk_token12"><td class="center_align">XYZ</td>
<input type="hidden" value="NN" id="stk_me12" name="stk_me12"><td class="center_align">11275</td>
<td class="center_align">-</td>
<td class="center_align">0.00</td>
<td class="center_align">-</td>
<td class="center_align">Item2</td>
<td class="center_align">Price</td>
<td class="center_align">Rs.35.40</td>
<td class="center_align">Dec 27 2015</td>
<td class="center_align">0.00</td>
<td class="center_align">0</td>
<td class="center_align">NN</td>
</tr>
</tbody></table>
</div></div>
</body>
</html></head> 

I need to get check box on the basis of checkbox name and its price like in given example, need to select checkbox name DELHI and price value Rs. 我需要在复选框名称及其价格的基础上获得复选框,如给定示例,需要选择复选框名称DELHI和价格值Rs。 35.40. 35.40。 These are two parameters available to select the correct checkbox. 这是两个可用于选择正确复选框的参数。

You can uniquely identify the checkbox based on the td element's text content. 您可以根据td元素的文本内容唯一地标识复选框。

So, in order to identify the checkbox corresponding to ' DELHI ', we can write the same statement with modified xpath as: 因此,为了识别与“ DELHI ”相对应的复选框,我们可以使用修改后的xpath编写相同的语句:

driver.find_element_by_xpath("//td[contains(.,'DELHI')]/input[@type='checkbox']").click() 

Answer to revised question under 'Update #2' 在“更新#2”下回答修订后的问题

If you want to select the checkbox based on the price, we can do it using the below xpath: 如果您想根据价格选择复选框,我们可以使用以下xpath进行选择:

//td[contains(., 'Rs.35.40')]/parent::tr/td[contains(.,'DELHI')]/input[@type='checkbox']

In this xpath, we are first identifying the cell (td) which contains the desired price (Rs.35.40), and based on it - we're identifying its parent (which is a 'tr'). 在此xpath中,我们首先确定包含所需价格(Rs.35.40)的单元格(td),然后基于该单元格-确定其父级(即“ tr”)。 Having identified the desired 'tr', we are then finding the desired checkbox which is located under the 'td' with text content 'DELHI'. 确定了所需的“ tr”后,我们便找到了所需的复选框,该复选框位于文本内容为“ DELHI”的“ td”下方。

Hence, your revised code, to select the 'DELHI' checkbox corresponding to the price 'Rs.35.40' would be as below: 因此,您的修改后的代码如下所示,以选中与价格“ Rs.35.40”相对应的“ DELHI”复选框:

driver.find_element_by_xpath("//td[contains(., 'Rs.35.40')]/parent::tr/td[contains(.,'DELHI')]/input[@type='checkbox']").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM