Selenium Python - 无法单击 iframe 中的复选框,该 iframe 绑定到带有在隐藏元素中更新的值的标签
Selenium Python - Unable to click on check box within an iframe which is bound to label with values updated in hidden element
我有一个表单页面,用于请求使用复选框访问服务器的 Password/RDP 之一或两者。默认选中 RDP。我也想检查密码。表格有一个框架,其中有
- 文本“访问请求”的标签
- 一个div有
- 默认情况下取值“2”的隐藏输入 *RDP 始终被选中)但如果密码也被选中,则更改为“2,1”
- 带有文本“密码”的标签绑定到复选框输入
- 带有文本“RDP”的标签绑定到复选框输入
我想单击 密码 的第一个复选框。
这里是html源代码:
<div class="control-group access-type">
<label class="control-label" data-bind="text: translate('view_new_requests:access_request_lbl', { defaultValue: 'Access Request:'})">Access Request:</label>
<div class="controls div-access-type" data-bind="visible: accessTypes().length > 0">
<input type="hidden" id="hiRequestAccessType" data-bind="value: requestAccessTypeStr" value="2">
<div class="retinaCS-validation-message" style="display: none; top: 210.812px; left: 183px; opacity: 0;">
<em></em>
<div style="display: table">
<div class="retinaCS-validation-message-text" style="display: table-cell" data-bind="validationMessage: field"></div>
</div>
</div>
<!-- ko foreach: $root.accessTypes -->
<label data-bind="css: 'checkbox' + ' ' + cssClassName()" class="checkbox viewpwd">
<input name="rbtnAccessType" type="checkbox" data-bind="value: bitValue(), enable: enabled(), checked: $root.requestAccessTypeStr" value="1">
<!-- ko text: title() -->Password<!-- /ko -->
</label>
<br>
<label data-bind="css: 'checkbox' + ' ' + cssClassName()" class="checkbox rdpaccess">
<input name="rbtnAccessType" type="checkbox" data-bind="value: bitValue(), enable: enabled(), checked: $root.requestAccessTypeStr" value="2">
<!-- ko text: title() -->RDP Session<!-- /ko -->
</label>
<br>
<!-- /ko -->
</div>
<div class="controls read-only-label" data-bind="visible: accessTypes().length == 0" style="display: none;">
<span class="form-value-label" data-bind="text: translate('view_new_requests:no_accessTypes_lbl', { defaultValue: 'No access request types available. Contact your administrator.'})">No access request types available. Contact your administrator.
</span>
</div>
</div>
注:
wait = ui.WebDriverWait(driver, 10)
其中 driver 是一个 Chromedriver 实例。另外,我已经切换到上述框架。
我尝试了以下方法:
1.Setting隐藏元素的值为'2,1':
passwordcheck_input_element = driver.find_element_by_css_selector("#hiRequestAccessType")
new_value = '2,1'
driver.execute_script("arguments[0].value = arguments[1].toString()", passwordcheck_input_element, new_value)
2.Click 直接在复选框上:
chckbox = wait.until(EC.element_to_be_clickable((By.NAME, "rbtnAccessType")))
或
chckbox = driver.find_element_by_xpath("//label/input[contains(..,'Password')]")
其次是
chckbox.click()
这是我看到的:
1.The 值成功更改为“2,1”,但未选中密码框,因此实际上没有任何反应。单击进入下一页时,仅在后端选择 RDP。
2.Nothing 发生了。没有错误,但也没有复选框操作。我认为它没有找到复选框元素,而是其他东西。如果我查询 chckbox.name,它 returns 错误说 WebElement 在我希望看到“rbtnAccessType”的地方没有属性“name”。
很大程度上取决于您如何切换到上述框架。
HTML 似乎是动态生成的,因此要在 <iframe>
标签中识别所需元素并与之交互,您必须:
- 诱导 WebDriverWait 以获得所需的 框架并切换到它.
- 诱导 WebDriverWait 使所需的 元素可点击。
- 您可以使用以下任一解决方案:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_css_selector")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.viewpwd>input[name='rbtnAccessType'][value='1']")))
使用XPATH
:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"iframe_xpath")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(@class,'viewpwd')]/input[@name='rbtnAccessType' and @value='1']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考文献:
我有一个表单页面,用于请求使用复选框访问服务器的 Password/RDP 之一或两者。默认选中 RDP。我也想检查密码。表格有一个框架,其中有
- 文本“访问请求”的标签
- 一个div有
- 默认情况下取值“2”的隐藏输入 *RDP 始终被选中)但如果密码也被选中,则更改为“2,1”
- 带有文本“密码”的标签绑定到复选框输入
- 带有文本“RDP”的标签绑定到复选框输入
我想单击 密码 的第一个复选框。
这里是html源代码:
<div class="control-group access-type">
<label class="control-label" data-bind="text: translate('view_new_requests:access_request_lbl', { defaultValue: 'Access Request:'})">Access Request:</label>
<div class="controls div-access-type" data-bind="visible: accessTypes().length > 0">
<input type="hidden" id="hiRequestAccessType" data-bind="value: requestAccessTypeStr" value="2">
<div class="retinaCS-validation-message" style="display: none; top: 210.812px; left: 183px; opacity: 0;">
<em></em>
<div style="display: table">
<div class="retinaCS-validation-message-text" style="display: table-cell" data-bind="validationMessage: field"></div>
</div>
</div>
<!-- ko foreach: $root.accessTypes -->
<label data-bind="css: 'checkbox' + ' ' + cssClassName()" class="checkbox viewpwd">
<input name="rbtnAccessType" type="checkbox" data-bind="value: bitValue(), enable: enabled(), checked: $root.requestAccessTypeStr" value="1">
<!-- ko text: title() -->Password<!-- /ko -->
</label>
<br>
<label data-bind="css: 'checkbox' + ' ' + cssClassName()" class="checkbox rdpaccess">
<input name="rbtnAccessType" type="checkbox" data-bind="value: bitValue(), enable: enabled(), checked: $root.requestAccessTypeStr" value="2">
<!-- ko text: title() -->RDP Session<!-- /ko -->
</label>
<br>
<!-- /ko -->
</div>
<div class="controls read-only-label" data-bind="visible: accessTypes().length == 0" style="display: none;">
<span class="form-value-label" data-bind="text: translate('view_new_requests:no_accessTypes_lbl', { defaultValue: 'No access request types available. Contact your administrator.'})">No access request types available. Contact your administrator.
</span>
</div>
</div>
注:
wait = ui.WebDriverWait(driver, 10)
其中 driver 是一个 Chromedriver 实例。另外,我已经切换到上述框架。
我尝试了以下方法:
1.Setting隐藏元素的值为'2,1':
passwordcheck_input_element = driver.find_element_by_css_selector("#hiRequestAccessType")
new_value = '2,1'
driver.execute_script("arguments[0].value = arguments[1].toString()", passwordcheck_input_element, new_value)
2.Click 直接在复选框上:
chckbox = wait.until(EC.element_to_be_clickable((By.NAME, "rbtnAccessType")))
或
chckbox = driver.find_element_by_xpath("//label/input[contains(..,'Password')]")
其次是
chckbox.click()
这是我看到的:
1.The 值成功更改为“2,1”,但未选中密码框,因此实际上没有任何反应。单击进入下一页时,仅在后端选择 RDP。
2.Nothing 发生了。没有错误,但也没有复选框操作。我认为它没有找到复选框元素,而是其他东西。如果我查询 chckbox.name,它 returns 错误说 WebElement 在我希望看到“rbtnAccessType”的地方没有属性“name”。
很大程度上取决于您如何切换到上述框架。
HTML 似乎是动态生成的,因此要在 <iframe>
标签中识别所需元素并与之交互,您必须:
- 诱导 WebDriverWait 以获得所需的 框架并切换到它.
- 诱导 WebDriverWait 使所需的 元素可点击。
- 您可以使用以下任一解决方案:
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_css_selector"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.viewpwd>input[name='rbtnAccessType'][value='1']")))
使用
XPATH
:WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"iframe_xpath"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(@class,'viewpwd')]/input[@name='rbtnAccessType' and @value='1']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考文献: