元素不可见,硒铬驱动程序,java



我正在尝试使用Selenium,但我遇到了找不到任何东西的问题。

我的代码如下所示:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Flows {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/mn/Desktop/project/turv/src/main/chromedriver");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://anywhere.c3a.dk/";  
driver.get(baseUrl);   
driver.findElement(By.id("login")).click();
driver.findElement(By.name("username")).sendKeys("placeholder");
driver.findElement(By.name("password")).sendKeys("placeholder");
driver.findElement(By.cssSelector("button.button.js-login-button")).click();
System.out.println(driver.getTitle());
driver.close();    
}
}

当我尝试与网站互动时,会出现此问题。简单的 driver.getTitle() 工作正常,但在尝试访问登录字段时出现错误。我已经尝试过By.nameBy.cssSelector.但两者都给出了相同的结果。我正在使用 Chrome 浏览器插件,因此请记录一些内容,然后尝试将其内容应用于我的代码,但是当我运行它时出现以下错误:

Starting ChromeDriver 2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011) on port 31943
Only local connections are allowed.
May 07, 2018 1:06:18 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
(Session info: chrome=66.0.3359.139)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'cetreas-MBP', ip: 'fe80:0:0:0:c9e:2c67:1d27:4e0b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.3', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.38.552518 (183d19265345f5..., userDataDir: /var/folders/s7/lv2wt4t15cn...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 66.0.3359.139, webStorageEnabled: true}
Session ID: 6d1cb5352073e7f1071830edf5b40106
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
at com.cetrea.flows.Flows.main(Flows.java:19)

我认为driver.findElement(By.id("login")).click();根本不是必要的,只是我尝试过的东西,因为它找不到另一个。

最有可能的是,WebDriver 在元素可见之前尝试访问该元素。我们需要等待一段时间才能看到它。

尝试:

driver.get("http://anywhere.c3a.dk/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username"))).sendKeys("placeholder");
driver.findElement(By.name("password")).sendKeys("placeholder");

尝试代替 css 选择器

driver.findElement(By.cssSelector("button.button.js-login-button")).click();

driver.findElement(By.className("button js-login-button")).click();

相关内容

  • 没有找到相关文章