我想使用来自 here 的测试数据进行自动测试与挪威的 BankId。但是我无法使用 Selenium 获取输入字段。
我尝试做的事情:
- 转到 https://www.banknorwegian.no/
- 点击“洛格客栈”
- 点击“BankID på mobil”。
- 点击“innlogging 的替代方案”下的“BankID”
- 输入“02105892090”(来自上面链接的测试用户)并点击“Logg inn”
- 在“Engangskode”中再次输入“02105892090”,然后点击提交按钮。
HTML:
<iframe frameborder="0" width="100%" height="100%" src="<URL>" title="BankID">
<div>Lots of divs...</div>
<input data-bind=" attr: { maxlength: maxlength, type: type, id: id, 'data-type': dataType, disabled: disabled, 'aria-disabled': disabled, 'pattern': pattern, 'inputmode': 'numeric', 'max': $data.max, 'min': $data.min, 'step': $data.step, 'tabindex': $data.tabIndex, 'aria-invalid': isInvalid, 'aria-label': label }, value: val, valueUpdate: valueUpdate, css: { error: $data.err, hasFocus: hasFocus, hideCaret: $data.hideCaret, hasValue: hasValue }, event: { focus: onFocus, blur: onBlur }" autocomplete="off" autocapitalize="off" autocorrect="off" formnovalidate="" required="" maxlength="255" type="password" id="qxaTy_DZXMJPMnP_rZae_2" tabindex="2000" aria-invalid="true" pattern="[0-9]*" class="">`
</iframe>
我可以到达 (6.) 但我无法获得 <input>
与 type="password"
在“Engangskode”下。它在 iframe
中这让事情变得更难了。这是我试过的:
public void EnterSsn(string ssn)
{
var driver = WebDriverFacade.GetDriver;
driver.SwitchTo().DefaultContent();
driver.SwitchTo().Frame(0);
Assert.IsTrue(driver.FindElement(By.CssSelector("input[type='password']")).ControlDisplayed());
driver.FindElement(By.CssSelector("input[type='password']")).SendKeysWrapper(ssn, "SSN");
}
但是我收到错误信息:
OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"css selector","selector":"input[type='password']"}
有人知道怎么做吗?
编辑:
在你们所有人的帮助下,这是最终有效的代码:
public void EnterSsn(string ssn)
{
var driver = WebDriverFacade.GetDriver;
driver.SwitchTo().DefaultContent();
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#ifmSingicat")));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("#bankid-container iframe")));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return document.querySelector(\".full_width_height\").shadowRoot.querySelector(\"input[type=\'password\']\")"))).SendKeys(ssn);
}
最佳答案
与文本 Engangskode 关联的字段在 #shadow-root (open)
中 在 child 身上有<iframe>
在父级内 <iframe>
.因此,要将字符序列发送到所需的字段,您需要:
- 诱导WebDriverWait父框架可用并切换到它。
- 诱导 WebDriverWait 子框架可用并切换到它。
- 为所需的
ElementToBeClickable()
引入 WebDriverWait在#shadow-root (open)
中.
您可以使用以下解决方案:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#ifmSingicat"))); new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe[title='BankID']"))); new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return document.querySelector('div.full_width_height').shadowRoot.querySelector('input')"))).SendKeys("02105892090");
浏览器快照:
关于c# - Selenium - 发送 key 以在影子根(打开)和挪威 BankId 的多个 iframe 中输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231027/