gpt4 book ai didi

java - 无法使用 Selenium WebDriver 在文本框中输入值

转载 作者:行者123 更新时间:2023-11-30 07:13:32 25 4
gpt4 key购买 nike

我正在使用 Selenium WebDriver 使用 java 在 2 textboxe 中输入文本。我可以在第一个文本框中输入文本,但对于第二个 textbox 我不断收到 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: 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:

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

Email_Console

我不明白在第二个文本框的情况下我哪里出了问题。 Angular 代码是否引起问题?请帮忙..

最佳答案

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

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");

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

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

Edit1:- 如果您想让元素可见,如果不确定 css 在您的元素中到底起作用,但下面是使元素可见的通用方法,如下所示:-

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 设置此值的东西如下:

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");

希望有帮助...:)

关于java - 无法使用 Selenium WebDriver 在文本框中输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38789445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com