Selenium-发送密钥以在影子根目录(打开)中输入密码,并为Norwegian BankId输入多个iframe

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

我想使用来自here的测试数据和挪威银行ID进行自动测试。但是我无法使用Selenium来掌握输入字段。

我尝试做的事情:

  1. 转到https://www.banknorwegian.no/
  2. 点击“登录旅馆”
  3. 单击“银行识别号”。
  4. 单击“用于登录的替代程序”下的“ BankID”
  5. 输入“ 02105892090”(通过上面的链接测试用户),然后单击“登录旅馆”
  6. 在“ 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.),但是随后我无法在“ Engangskode”下的<input>中抓住type="password"。它位于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']"}

有人知道如何执行此操作吗?

c# selenium iframe shadow-dom webdriverwait
2个回答
0
投票

这里您有2个iframe(嵌套iframe),因此您需要切换两次。首先切换到ID = ifmSingicat的iframe,然后再切换到已切换iframe的第一个iframe。

//Main document
driver.SwitchTo().DefaultContent();

//Find the first frame, and use switch to frame
IWebElement containerFrame = driver.FindElement(By.Id("ifmSingicat"));
driver.SwitchTo().Frame(containerFrame);

//You are now in iframe "containerFrame", now find the nested iframe
IWebElement contentFrame = driver.FindElement(By.CssSelector("#bankid-container iframe"));
driver.SwitchTo().Frame(contentFrame);

//Now find the elements you want in the nested frame
IWebElement foo = driver.FindElement(By.CssSelector("input[type='password']"));

注意:我不是C#开发人员,希望以上语法正确。


0
投票

如果切换到正确的iframe,则只需要添加一些等待即可。这将为iframe元素提供一些加载时间。

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