如何使用 Selenium 和 Python 定位复选框以验证其状态
How to locate a checkbox to verify its state using Selenium and Python
我想弄清楚我们网站上的复选框是否处于选中状态。复选框的 HTML 如下:
<input id="ID_StaffIsRostered" name="ID_Rostering" type="checkbox" checked="" data-toggle="toggle" data-onstyle="success" data-size="sm" data-on="Yes" data-off="No" onchange="toggleRosteredStaff()">
我试过通过这些定位器找到它:
(By.ID, "ID_StaffIsRostered")
(By.XPATH, "input[@name='ID_Rostering']"
(By.XPATH,"//*[@id='ID_StaffIsRostered']")
(By.XPATH,"/html/body/div[1]/div/div[2]/div/div/div/div[2]/table[3]/tbody/tr/td/div[1]/div[2]/div/form/div/div[1]/div/label/div/input")
(By.CSS_SELECTOR,"label:nth-child(1) > .btn-light .toggle-off")
(By.XPATH,"//form[@id='ID_Form_Rostering']/div/div/div/label/div/div/label[2]")
(By.XPATH,"//div/div/div/label/div/div/label[2]")
到目前为止没有任何效果。但是,我可以 select 包含复选框的 div,以及复选框根据切换到的内容而切换的标签,HTML 是:
<div class="toggle btn btn-success btn-sm" data-toggle="toggle" style="width: 7.5px; height: 26px; min-width: 38px;">
对于标签:
<div class="toggle-group"><label class="btn btn-success btn-sm toggle-on">Yes</label><label class="btn btn-light btn-sm toggle-off">No</label><span class="toggle-handle btn btn-light btn-sm"></span></div>
有哪些替代方法可以实现这一点?目前我正在使用 Selenium 4.0.0 和 Python 3.9
- checkbox的checked参数会表示是否被选中,可以通过xpath获取该参数的状态(.../input[@checked])
- 注意遵守网站法律,勾选的复选框是否有特殊状态,如颜色、文字等
有办法我能想到。
element.get_attribute('checked')
或者您可以查询javascript.
return document.querySelector('input[name="ID_Rostering"]').checked;
至于尝试 xpath 但它并不总是可靠的。
//input[@type='checkbox' and not(@checked)]
未勾选
//input[@type='checkbox' and @checked)]
选中
根据 HTML Specification for Boolean attributes:
A number of attributes are Boolean attributes. The presence of a
boolean attribute on an element represents the true value, and the
absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string
or a value that is an ASCII case-insensitive match for the attribute's
canonical name, with no leading or trailing whitespace.
checked 属性可以用以下任一方式表示:
<input name=name id=id type=checkbox checked>
<input name=name id=id type=checkbox checked="">
<input name=name id=id type=checkbox checked="checked">
解决方案
要验证复选框的状态,您可以探测 checked
属性,您可以使用以下代码块:
try:
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#ID_StaffIsRostered[name='ID_Rostering'][checked]")))
print("Checkbox is in checked state")
except:
print("Checkbox is in unchecked state")
我想弄清楚我们网站上的复选框是否处于选中状态。复选框的 HTML 如下:
<input id="ID_StaffIsRostered" name="ID_Rostering" type="checkbox" checked="" data-toggle="toggle" data-onstyle="success" data-size="sm" data-on="Yes" data-off="No" onchange="toggleRosteredStaff()">
我试过通过这些定位器找到它:
(By.ID, "ID_StaffIsRostered")
(By.XPATH, "input[@name='ID_Rostering']"
(By.XPATH,"//*[@id='ID_StaffIsRostered']")
(By.XPATH,"/html/body/div[1]/div/div[2]/div/div/div/div[2]/table[3]/tbody/tr/td/div[1]/div[2]/div/form/div/div[1]/div/label/div/input")
(By.CSS_SELECTOR,"label:nth-child(1) > .btn-light .toggle-off")
(By.XPATH,"//form[@id='ID_Form_Rostering']/div/div/div/label/div/div/label[2]")
(By.XPATH,"//div/div/div/label/div/div/label[2]")
到目前为止没有任何效果。但是,我可以 select 包含复选框的 div,以及复选框根据切换到的内容而切换的标签,HTML 是:
<div class="toggle btn btn-success btn-sm" data-toggle="toggle" style="width: 7.5px; height: 26px; min-width: 38px;">
对于标签:
<div class="toggle-group"><label class="btn btn-success btn-sm toggle-on">Yes</label><label class="btn btn-light btn-sm toggle-off">No</label><span class="toggle-handle btn btn-light btn-sm"></span></div>
有哪些替代方法可以实现这一点?目前我正在使用 Selenium 4.0.0 和 Python 3.9
- checkbox的checked参数会表示是否被选中,可以通过xpath获取该参数的状态(.../input[@checked])
- 注意遵守网站法律,勾选的复选框是否有特殊状态,如颜色、文字等
有办法我能想到。
element.get_attribute('checked')
或者您可以查询javascript.
return document.querySelector('input[name="ID_Rostering"]').checked;
至于尝试 xpath 但它并不总是可靠的。
//input[@type='checkbox' and not(@checked)]
未勾选
//input[@type='checkbox' and @checked)]
选中
根据 HTML Specification for Boolean attributes:
A number of attributes are Boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
checked 属性可以用以下任一方式表示:
<input name=name id=id type=checkbox checked>
<input name=name id=id type=checkbox checked="">
<input name=name id=id type=checkbox checked="checked">
解决方案
要验证复选框的状态,您可以探测 checked
属性,您可以使用以下代码块:
try:
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#ID_StaffIsRostered[name='ID_Rostering'][checked]")))
print("Checkbox is in checked state")
except:
print("Checkbox is in unchecked state")