我有脚本登录到站点,然后等待主页按钮加载。我已经将fluentwait等待元素可见。但是,由于未加载主页按钮,WebDriver并没有立即等待并失败。
我尝试了fluentwait和webdriverwait,但是它们都没有起作用。
这是我正在尝试的代码。
By usernameInput = By.name("User”);
By passwordInput = By.name("Password”);
By loginButton = By.xpath("//input[@value='Sign In']”);
String URL = “https://demotest.mytest.com/"
WebDriver driver = new SafariDriver();
driver.get(URL);
driver.manage().window().maximize();
setText(usernameInput,”User1");
setText(passwordInput,”User1");
clickOnElement(loginButton);
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“ // div [@ title ='Home']”))));;
抛出以下错误-
org.openqa.selenium.WebDriverException:
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'cnparmar-mac', ip: '10.168.104.60', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.safari.SafariDriver
Capabilities {acceptInsecureCerts: false, browserName: Safari, browserVersion: 12.1.1, javascriptEnabled: true, platform: MAC, platformName: MAC, safari:automaticInspection: false, safari:automaticProfiling: false, safari:diagnose: false, setWindowRect: true, strictFileInteractability: false, webkit:WebRTC: {DisableICECandidateFiltering: false, DisableInsecureMediaCapture: false}}
Session ID: 1257BF8B-335E-4E80-8B08-CB15A77AD648
*** Element info: {Using=xpath, value=//div[@title='Home']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructo rAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingCo nstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException( W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpRes ponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpRes ponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExec utor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(Drive rCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.jav a:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver .java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWe bDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver .java:315)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedCond itions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedCond itions.java:201)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
at sample.Sample.loginAs(Sample.java:64)
at sample.Sample.test1(Sample.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.j ava:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccess orImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocati onHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorke r.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
理想情况下,它应该等待“主页”按钮可见。但是失败了。这适用于Windows上的Chrome,Firefox,IE,Edge,但仅适用于MAC上的Safari。
答案 0 :(得分:0)
您似乎正在混合使用WebDriverWait和FluentWait类。
尝试使用以下内容代替
:WebDriverWait wait = new WebDriverWait(driver, 15, 100);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@title='Home']")));