我想使用来自here的测试数据和挪威银行ID进行自动测试。但是我无法使用Selenium来掌握输入字段。
我尝试做的事情:
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']"}
有人知道如何执行此操作吗?
这里您有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#开发人员,希望以上语法正确。
如果切换到正确的iframe,则只需要添加一些等待即可。这将为iframe元素提供一些加载时间。