硒的Python - 无法被绑定在隐藏的元素更新值标注在iframe中点击复选框

问题描述 投票:1回答:1

我有一个请求使用复选框服务器密码/ 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' + '&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)

其中,驾驶员是一个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.Nothing发生。没有错误,但不复选框行动无论是。我认为它没有找到该复选框元素,但别的东西。如果我查询chckbox.name,则返回错误说WebElement有,我希望看到“rbtnAccessType”无属性“名”。

python selenium xpath css-selectors webdriverwait
1个回答
1
投票

这在很大程度上取决于你如何切换到上述框架。

该HTML似乎是动态生成的,因此识别和你所拥有的<iframe>标签内所需的元素进行交互:

  • 诱导WebDriverWait为所需的帧可用,并切换到它。
  • 诱导WebDriverWait为所需的元素到点击。
  • 您可以使用以下解决方案:
  • 使用CSS_SELECTORWebDriverWait(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']")))
  • 使用XPATHWebDriverWait(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

参考文献:

© www.soinside.com 2019 - 2024. All rights reserved.