如何将文本插入texbox硒C#

时间:2018-08-15 04:43:01

标签: c# selenium selenium-webdriver

    <div class="input-container">

                <div class="form-group">
                    <div class="labelContainer">
                        <<p>Current Password<span>*</span></p>
                    </div>
                    <label for="currentPassword" class="sr-only">Current Password</label>
                    <input type="password" id="currentPassword" name="currentPassword" class="form-control" placeholder="Current Password" required="">
                    <i class="fa fa-lock icon-style" aria-hidden="true"></i>
                </div>

                <div class="form-group">
                    <div class="labelContainer">
                        <<p>Enter New Password<span>*</span></p>
                    </div>
                    <label for="newPassword" class="sr-only">New Password</label>
                    <input type="password" id="newPassword" name="newPassword" class="form-control" placeholder="New Password" required="">
                    <i class="fa fa-lock icon-style" aria-hidden="true"></i>
                </div>

                <div class="form-group">
                    <div class="labelContainer">
                        <<p>Confirm New Password<span>*</span></p>
                    </div>
                    <label for="confirmPassword" class="sr-only">Confirm New Password</label>
                    <input type="password" id="confirmPassowrd" name="confirmPassowrd" class="form-control" placeholder="Confirm Password" required="">
                    <i class="fa fa-lock icon-style" aria-hidden="true"></i>
                </div>

上面是html代码,我想将当前密码插入当前密码文本字段。我在下面尝试了两种方法,它说元素不可见,这是行不通的。

(code 1 )driver.FindElement(By.Id("currentPassword")).SendKeys("testabc");
(code 2 )IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("document.getElementById('currentPassword').value='testabc';");

尝试测试newPassword文本字段而不是currentPassword文本字段,它工作正常。只有currentPassword和ConfirmPassword文本字段具有不可见的错误消息元素。

有人知道是什么问题吗?

1 个答案:

答案 0 :(得分:3)

您可以尝试

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("currentPassword"))).SendKeys("testabc");