1

I have a form page for requesting access to one or both of Password/RDP to a server using checkboxes. RDP is checked by default. I want to check Password as well. The form has a frame within which there is

  • a label for text "Access Request"
  • a div that has
    • a hidden input that takes value "2" by default *RDP is always checked) but changes to "2,1" if Password is also checked
    • a label with text "Password" bound to an input which is a checkbox
    • a label with text "RDP" bound to an input which is a checkbox

I want to click on the first checkbox for Password.

Here is html source code:

<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>

Note:

wait = ui.WebDriverWait(driver, 10)

where driver is a Chromedriver instance. Also, I have switched to aforementioned frame.

I tried the following:

1.Setting the value of hidden element to '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 on checkbox directly:

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

OR

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

followed by

chckbox.click()

This is what I see:

1.The value changes successfully to '2,1' but the box for password is not checked so nothing really happens. It is only RDP that is chosen at the back end upon clicking through to the next page.

2.Nothing happened. No error but no checkbox action either. I think it didn't find the checkbox element but something else. If I query chckbox.name, it returns error saying WebElement has no attribute "name" where I expected to see "rbtnAccessType".

Community
  • 1
  • 1
Titanical
  • 45
  • 1
  • 2
  • 9

1 Answers1

2

A lot depends on how you have switched to aforementioned frame.

The HTML seems to be dynamically generated, so to identify and interact with the desired element within the <iframe> tag you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use either of the following solutions:
  • Using 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']")))
    
  • Using 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']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This worked. I googled around a bit but couldn't find the CSS_SELECTOR you provide above for label and input. Particularly, "label.viewpd>..." format. Using copy selector from browser tools doesn't give it either. A bit frustrating. – Titanical Feb 10 '19 at 13:05
  • @Titanical I am sure with a finer search within StackOverflow you will find a handful of my answers _accepted_ and _upvoted_ based on this concept which is well documented in this [documentation](https://www.w3schools.com/tags/att_label_for.asp) and in this [discussion](https://stackoverflow.com/questions/12894169/what-is-the-html-for-attribute-in-label) – undetected Selenium Feb 10 '19 at 13:20