无法使用Selenium和Python找到Checkbox元素

时间:2016-09-21 16:48:03

标签: python-3.x selenium

Target Checkbox

这是单独的Checkbox的HTML代码:



<table id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True" border="0">
			<tbody><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_0" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$0"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_0">Cracked Case</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_1" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$1"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_1">Cracked Case (Upper Half)</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_2" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$2"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_2">Cracked Face Plate</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_3" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$3"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_3">Deep Scratches</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_4" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$4"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_4">Discolored Case</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_5" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$5"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_5">Light Scratches In Case</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_6" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$6"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_6">Major Cracks</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_7" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$7"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_7">Major Scratches</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_8" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$8"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_8">Minor Cracks</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_9" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$9"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_9">Minor Scratches</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_10" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$10"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_10">Missing Port Cover</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_11" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$11"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_11">Missing Side Panel</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_12" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$12"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_12">No Back Cover</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_13" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$13"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_13">No Bay Cover</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_14" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$14"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_14">No Face Plate</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_15" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$15"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_15">Open Box</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_16" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$16"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_16">Open Box, Sealed Unit</label></td><td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_17" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$17"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_17">Scratches In Screen</label></td>
			</tr><tr>
				<td><input id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_18" type="checkbox" name="rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38788_24582_73_0_True$18"><label for="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38788_24582_73_0_True_18">Sealed Box</label></td><td></td>
			</tr>
		</tbody></table>
&#13;
&#13;
&#13;

大家好!问题是简单而复杂的同时,在图片是我的目标点击,一个复选框,无论如何应该很容易定位但由于某种原因Python不断抛出我这个错误(我以前遇到过但现在我不能绕过它):

Traceback (most recent call last):
  File "C:\Users\AMSUser\AppData\Local\Programs\Python\Python35-32\Scripts\Add Asset.py", line 167, in <module>
    nobackcover = browser.find_element_by_xpath('//*[@id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38774_24566_73_0_True_12"]')
  File "C:\Users\AMSUser\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\AMSUser\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']
  File "C:\Users\AMSUser\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Users\AMSUser\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38774_24566_73_0_True_12"]"}
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 10.0.14393 x86_64)

或者如果我使用名称或ID来定位复选框的变体。

当我使用Chrome开发人员复制检查元素之后的xpath时 工具这就是我得到的:

"//*[@id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38760_24576_73_0_True_12"]".

这是我目前使用的代码:

#Here i open the checkbox menu that gives me the checkbox i need to click
Cosmeticselect = browser.find_element_by_xpath('//*[@id="rmaAssetCosmeticComponents_ctl00_btnAdd"]')
Cosmeticselect.click()
#2 seconds delay because it takes a second to load.
time.sleep(2)
#I have tried several iterations of this none give me the desire output, maybe this checkbox is in another iframe? if so i have yet to find it in the code.
nobackcover = browser.find_element_by_id('rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38776_24568_73_0_True_12')
#nobackcover = browser.find_element_by_xpath('//*[@id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38775_24567_73_0_True"]/tbody/tr[7]/td[1]/label')
#nobackcover = browser.find_element_by_name('rmaAssetCosmeticComponents$ctl00$CheckBoxList_Comp_17_38778_24570_73_0_True$12')
nobackcover.click()

我怀疑我必须转向化妆品&#34;窗口&#34;但我还没有找到iframe等的参考,我将继续努力,当我找到我的解决方案我会发布它但如果有人有兴趣为这个问题添加一双新眼睛我会很感激

2 个答案:

答案 0 :(得分:0)

首先,确保您的路径(//[@id="rmaAssetCosmeticComponents_ctl00_CheckBoxList_Comp_17_38774_24566_73_0_True_12")可以使用Selenium IDE

指向正确的元素

其次,使用WebElement等待元素可点击或存在元素位于

答案 1 :(得分:0)

在使用html代码更新之前,您可以尝试以下方法:

的CSS
input[id*='rmaAssetCosmeticComponents'][id*='True_12']

的xpath&#39; S
//input[text()='No Back Cover']
//input[contains(@id, 'rmaAssetCosmeticComponents')][contains(@id, 'True_12')]

跳转id的最后一部分不会改变,你可以根据ht属性的部分创建一个不会改变的选择器,或者基于元素中包含的文本。