无法使用Selenium WebDriver在文本框中输入值 [英] Not able to enter value in Textbox using Selenium WebDriver

查看:1479
本文介绍了无法使用Selenium WebDriver在文本框中输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java使用Selenium WebDriver在2 textboxe中输入文本.我可以在第一个文本框中输入文本,但是对于第二个textbox我一直得到ElementNotVisibleException

I'm using Selenium WebDriver to enter text in 2 textboxe using java. I am able to enter text in the 1st text box but for the 2nd textbox I keep getting ElementNotVisibleException

HTML代码 ..

<form name="form">
  <div class="form-group">
      <input name="name" type="text" class="form-control" ng-model="name" placeholder="Name*" required>
   </div>
   <div class="form-group">
       <input name="email" id="email" type="email" class="form-control" ng-model="email" ng-change="(form.email.$dirty && form.email.$valid) ? error='' : error='Please enter a valid email'" placeholder="Email*" required autofocus/>
    </div>
</form>

Java代码 ..

WebElement name = driver.findElement(By.name("name"));  
name.sendKeys("Sample Name");

WebElement signup_email = driver.findElement(By.name("email")); 
signup_email.sendKeys("abc@xyz.com");

错误:

org.openqa.selenium.ElementNotVisibleException:元素不是 当前可见,因此可能无法与Command进行交互 持续时间或超时:19毫秒版本信息:版本:'2.53.1', 版本:'a36b8b1',时间:'2016-06-30 17:32:46'系统信息:主机: 'Hp-PC',ip:'172.16.255.131',os.name:'Windows 7',os.arch:'amd64', os.version:"6.1",java.version:"1.7.0_79"会话ID: 2e7ab2fd-cd6b-428e-86e7-a4f7d8d737fa驱动程序信息: org.openqa.selenium.firefox.FirefoxDriver功能 [{platform = WINDOWS,acceptSslCerts = true,javascriptEnabled = true, cssSelectorsEnabled = true,databaseEnabled = true,browserName = firefox, handlesAlerts = true,nativeEvents = false,webStorageEnabled = true, rotatable = false,locationContextEnabled = true, applicationCacheEnabled = true,takesScreenshot = true,版本= 47.0.1}] 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:526)在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) 在 org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:122) 在src.tests.EmailSignUp.test(EmailSignUp.java:107)处 src.tests.EmailSignUp.main(EmailSignUp.java:27)

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 19 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46' System info: host: 'Hp-PC', ip: '172.16.255.131', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_79' Session ID: 2e7ab2fd-cd6b-428e-86e7-a4f7d8d737fa Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=47.0.1}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:122) at src.tests.EmailSignUp.test(EmailSignUp.java:107) at src.tests.EmailSignUp.main(EmailSignUp.java:27)

编辑1 :

在浏览器控制台中,以下类由角Js自动填充

In the Browser Console the following classes are being automatically being populated by angular Js

在第二个textbox的情况下,我不知道哪里出了问题.角度代码会引起问题吗?请帮忙.

I can't understand where I'm going wrong in case of 2nd textbox. Is the angular code causing problems?? Please Help..

推荐答案

可能是当您要定位元素时,它不会出现在DOM上,您应该尝试使用WebDriverWait等到元素出现如下:-

May be when you are going to locate element, it would not be present on the DOM, You should try using WebDriverWait to wait until element present as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement name = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("name")));
name.sendKeys("Sample Name");

WebElement signup_email = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("email")));
signup_email.sendKeys("abc@xyz.com");

Edited1 :-如果在sendKeys()期间仍然遇到问题,我认为您的元素中嵌入了一些javascript,使其不可见,在这种情况下,请尝试使用JavascriptExecutor设置值在下面:-

Edited1 :- If you are still facing the issue during sendKeys(), I think some javascript embedded in your element which makes it invisible, in this case try to set value using JavascriptExecutor as below :-

WebElement signup_email = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("email")));
((JavascriptExecutor)driver).executeScript("arguments[0].value = 'abc@xyz.com'", signup_email);

Edited1 :-如果要使元素可见,则不确定元素中css的确切工作方式,但以下是通用的制作方法,如下所示:-

Edited1:- If you want to make element visible, if not sure what exactly css work in your element but below is the generic way to make is visible as below :-

WebElement signup_email = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("email")));
signup_email = (WebElement)((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; return arguments[0];", signup_email);

//Now if it is visible then goto set value
signup_email.sendKeys("abc@xyz.com");

已编辑:-正如我所见,该元素适用于angularjs,请尝试使用angularjs的东西使用JavascriptExecutor对此值进行设置,如下所示:

Edited :- As I see this element is works with angularjs, try angularjs stuff to set value on this using JavascriptExecutor as below :

WebElement signup_email = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("email")));

((JavascriptExecutor).executeScript("angular.element(arguments[0]).scope().email = arguments[1]", signup_email, "abc@xyz.com");

希望有帮助...:)

这篇关于无法使用Selenium WebDriver在文本框中输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆