2015-02-10 27 views
1

我试图检查所有复选框与硒的webdriver的数组。但是我正在努力寻找正确的方法/函数,不知何故,这整个代码很混乱。Python的硒阵列

我已经有一个或多个数据箱这样的:

<div class="data_row"> 
<span id="checkbox_detail_ONE" class="checkbox_detail"> 
<input type="checkbox" value="dir://TEST" name="file_list" onclick="check_enable_btn()"> 
从那里我要检查所有的复选框,除了一个名叫 checkbox_detail_ONE

在Python中我尝试这样

,但我猜它对曲解的误解:driver.find_elements_by_xpath()

for i in driver.find_elements_by_xpath("//span[@class='checkbox_detail']"): 
    if "ONE" in i.text: 
     print "Keep " 
    else: 
     print "Delete" 
     i.click() 

回答

2

尝试你的代码的各个位之后,他们似乎大部分工作。您的问题可能是排除了check_detail_ONE复选框。 i.text返回元素中包含的文本,但是从您发布的代码中没有任何东西让我相信复选框将在其文本中包含一个。 对于您的排除,您需要使用专门选择此复选框的选择。硒支持选择的ID,所以你可以使用以下命令:

driver.find_element_by_id("checkbox_detail_ONE") 

或者,如果你喜欢使用的XPath:

driver.find_element_by_xpath("@id='checkbox_detail_ONE'") 

如果你真的要确保这是一个跨度:

driver.find_element_by_xpath("//span[@id='checkbox_detail_ONE']") 

这将导致下面的代码:

for i in driver.find_elements_by_xpath("//span[@class='checkbox_detail']"): 
    if i == driver.find_element_by_xpath("//span[@id='checkbox_detail_ONE']"): 
     print "Keep " 
    else: 
     print "Delete" 

现在,因为我们将检查盒子,它们是两个不同的元素,我们需要 比较输入元素,而不是它们包含在跨度的,我们可以选择那些使用XPath的/descendant::功能,导致下面的最终代码:

for i in driver.find_elements_by_xpath("//span[@class='checkbox_detail']/descendant::input"): 
    if i == driver.find_element_by_xpath("//span[@id='checkbox_detail_ONE']/descendant::input"): 
     pass # no action required 
    else: 
     i.click() 
+0

经过一点typocorrection这个为我工作,谢谢!可惜我不能在修改错字'因为我在driver.find_elements_by_xpath'我会接受的解决方案,您编辑后 - 不,它不能再更改。 – schiggn 2015-02-13 07:58:43

+0

看来我失去了一个“s”的地方,在那里任何其他错字的? – Cronax 2015-02-13 09:32:21

+0

不行,现在一切都好!再次感谢。 – schiggn 2015-02-13 11:20:34

1

你可以得到所有chec除“checkbox_detail_ONE”一个一气呵成kboxes使用XPath:

driver.find_elements_by_xpath("//span[@class = 'checkbox_detail' and @id != 'checkbox_detail_ONE']") 

这将返回你除了有“checkbox_detail_ONE”的名字一个都span元素。

如果你需要从这里input元素,你可以使用following-sibling

//span[@class = 'checkbox_detail' and @id != 'checkbox_detail_ONE']/following-sibling::input 
+0

谢谢您的输入。我没有验证你的解决方案,因为另一个建议为我解决。但我认为这让我更好地理解xpath。 – schiggn 2015-02-13 08:02:16