我正在尝试使用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.name
和By.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();
是必要的,只是我尝试过的东西,因为它无法找到另一个。
答案 0 :(得分:2)
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");
答案 1 :(得分:0)
尝试代替css选择器
driver.findElement(By.cssSelector("button.button.js-login-button")).click();
此
driver.findElement(By.className("button js-login-button")).click();