python - Selenium Python - 无法单击 iframe 中的复选框,该复选框绑定(bind)到带有隐藏元素中更新的值的标签

标签 python selenium xpath css-selectors webdriverwait

我有一个表单页面,用于使用复选框请求访问服务器的密码/RDP 之一或两者。默认情况下会选中 RDP。我也想检查密码。该表单有一个框架,其中有

  • 文本“访问请求”的标签
  • 一个 div 具有
    • 默认情况下采用值“2”的隐藏输入 *RDP 始终处于选中状态),但如果还选中了“密码”,则更改为“2,1”
    • 带有文本“密码”的标签,绑定(bind)到作为复选框的输入
    • 带有文本“RDP”的标签绑定(bind)到作为复选框的输入

我想点击密码的第一个复选框。

这是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' + '&nbsp;' + 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' + '&nbsp;' + 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.设置隐藏元素的值为'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.直接点击复选框:

chckbox = wait.until(EC.element_to_be_clickable((By.NAME, "rbtnAccessType")))

或者

chckbox = driver.find_element_by_xpath("//label/input[contains(..,'Password')]")

其次是

chckbox.click()

这就是我所看到的:

1.值成功更改为“2,1”,但密码框未选中,因此什么也没有发生。单击进入下一页时,后端仅选择 RDP。

2.什么也没发生。没有错误,但也没有复选框操作。我认为它没有找到复选框元素,而是找到了其他元素。如果我查询 chckbox.name,它会返回错误,指出 WebElement 没有属性“name”,而我希望在其中看到“rbtnAccessType”。

最佳答案

很大程度上取决于您如何切换到上述框架

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
    
<小时/>

引用文献:

关于python - Selenium Python - 无法单击 iframe 中的复选框,该复选框绑定(bind)到带有隐藏元素中更新的值的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54608658/

相关文章:

python - 如何在 xpath 命令中使用 python scrapy 进行网络抓取的任意值

c# - 使用 selenium C# 在一行中单击操作链接(删除、查看、编辑)

xpath - XPath查询节点名称中带有冒号的XML节点

python - 从子模块导入 Python 包时避免 pylint 投诉

python - 无法在Python中使用selenium在Angular JS中定位元素(文本字段)

python - 在python中高效处理~5000万条记录文件

Python Selenium 网络驱动程序 : Does not scroll down the page if browser is minimised

python - 通过css选择器python,selenium使用查找元素时选择内部文本并创建循环

python - ValueError : Cannot feed value of shape (2, ) 对于张量 'Placeholder:0' ,其形状为 '(1, 2)'

python - 使用 scipy.io 时出现导入错误