c# - Selenium - 发送 key 以在影子根(打开)和挪威 BankId 的多个 iframe 中输入密码

标签 c# selenium iframe shadow-dom webdriverwait

我想使用来自 here 的测试数据进行自动测试与挪威的 BankId。但是我无法使用 Selenium 获取输入字段。

我尝试做的事情:

  1. 转到 https://www.banknorwegian.no/
  2. 点击“洛格客栈”
  3. 点击“BankID på mobil”。
  4. 点击“innlogging 的替代方案”下的“BankID”
  5. 输入“02105892090”(来自上面链接的测试用户)并点击“Logg inn”
  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.) 但我无法获得 <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) 中.

shadowDOM_password_field

  • 您可以使用以下解决方案:

    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");
    
  • 浏览器快照:

shadowDOM_password

关于c# - Selenium - 发送 key 以在影子根(打开)和挪威 BankId 的多个 iframe 中输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231027/

相关文章:

c# - 获取传递给 C# 方法的参数的名称

java - 在 selenium 中使用 FindBy 时出错。报错信息空点异常

javascript-如何检测 iframe 中的滚动事件?

python - 如何使用 Xpath 在 iframe 中选择元素?

jquery - Wmode = transparent和autoplay = 1不能一起使用

c# - 将TSQL代码转换为C#数据类型问题

c# - 使用 powershell 脚本获取功能键时出错

c# - 在 Windows Phone 8.1 的 sqlite 中进行泛化 CRUD 操作

python - Selenium 在生产中仅在 headless 模式下工作是真的吗? ( Django )

python - 让selenium在python中使用默认配置文件启动safari