![](/img/trans.png)
[英]Python Selenium: Unable to find xpath to click on element within iframe
[英]Selenium Python - Unable to click on check box within an iframe which is bound to label with values updated in hidden element
我有一個表單頁面,用於使用復選框請求訪問服務器的密碼/RDP 之一或兩者。 默認情況下選中 RDP。 我也想檢查密碼。 表格有一個框架,其中有
我想點擊Password的第一個復選框。
這是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.將隱藏元素的值設置為'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 沒有我希望看到“rbtnAccessType”的屬性“name”。
很大程度上取決於您如何切換到上述框架。
HTML 似乎是動態生成的,因此要識別<iframe>
標記中所需的元素並與之交互,您必須:
使用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
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.