我编写了自动鼠标悬停操作的代码: -
Actions action=new Actions(driver);
WebElement product=driver.findElement(By.xpath("//*[@id='Products']/em"));
WebElement catalogue=driver.findElement(By.xpath("//*[text()='Master Catalog']"));
Actions builder = new Actions(driver);
Action mouseOverMenu = builder.moveToElement(product).build();
mouseOverMenu.perform();
Thread.sleep(3000L);
catalogue.click();
我想首先悬停'产品' webelement和hovering'产品' webelement' catalog'出现并希望点击' catalog' webelement。
但是此代码抛出异常,请建议: -
org.openqa.selenium.ElementNotInteractableException:
Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
System info: host: 'DESKTOP-0HLH9L7', ip: '192.168.168.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\lenovo\AppData\Local\Temp\rust_mozprofile.Vz7r3MDL9N0a, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=54.0.1, platformVersion=10.0, moz:processID=528, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: cc3875ef-61fd-422c-86cb-3647fbf03d93
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)
at actionsClass.MoveTo.Moving(MoveTo.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
答案 0 :(得分:0)
尝试如下:
Actions builder = new Actions(driver);
builder.moveToElement(product).moveToElement(catalogue).click().build().perform();
编辑1:
Actions act1 = new Actions(driver);
Actions act2 = act1.moveToElement(product).build();
act2.moveToElement(catalogue).click().build().perform();
编辑2:
Actions act1 = new Actions(driver);
Actions act2 = new Actions(driver);
act1.moveToElement(product).build();
Thread.sleep(4000); //you can change this to explicit wait later
act2.moveToElement(catalogue).click().build().perform();
答案 1 :(得分:0)
我通过以下代码找到了分辨率,并且在所有浏览器中都可以正常工作:
WebElement元素= driver.findElement(By.linkText(“产品类别”));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebElement subElement = driver.findElement(By.linkText("iPads"));
action.moveToElement(subElement);
action.click();
action.perform();
//说明: / 在某些浏览器中,一旦执行了鼠标悬停操作,菜单列表就会消失,在Selenium识别下一个子菜单项并对它执行单击操作之前,几秒钟的时间。在这种情况下,最好在主菜单上使用“ perform()”操作来保持菜单列表,直到Selenium确定子菜单项并单击它为止。 /