Selenium java代码,用于单击Mozilla FireFox中的禁用选项卡

时间:2017-11-22 08:56:14

标签: java selenium

我是测试人员,最近开始使用硒自动进行现场测试。如果我的问题不符合标记或不相关或重复,请原谅我。

Snippet of the site

在附件中,您可以看到启用“概述”选项卡时,其余两个选项卡被禁用,即“昨天”和“统计”。在页面加载时,启用“概览”选项卡并显示内容。但是我需要在页面加载时单击“统计”选项卡。

我尝试了下面的代码,但它没有用。任何人都可以帮助我,我会非常感激。

代码尝试了:

driver.findElement(By.xpath("//a[@title='Statistics']")).click();

WebDriverWait wait0 = new WebDriverWait(driver, 50);
WebElement ElementBtn0 = driver.findElement(By.xpath(".//*[@id='WD48']"));
wait0.until(ExpectedConditions.elementToBeClickable(ElementBtn0));
ElementBtn0.click();

跟踪日志:

1511342545379   geckodriver INFO    geckodriver 0.19.1
1511342545433   geckodriver INFO    Listening on 127.0.0.1:10000
1511342546659   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\abcd\\AppData\\Local\\Temp\\rust_mozprofile.8ZsWdwktZ3Ao"
1511342549254   Marionette  INFO    Enabled via --marionette
Unable to read VR Path Registry from C:\Users\abcd\AppData\Local\openvr\openvrpaths.vrpath
[Child 18796, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 18796, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
1511342554313   Marionette  INFO    Listening on port 2000
1511342554659   Marionette  WARN    TLS certificate errors will be ignored for this session
1511342554829   Marionette  DEBUG   Register listener.js for window 4294967297
Nov 22, 2017 2:52:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1511342555319   Marionette  DEBUG   Received DOM event "beforeunload" for "about:blank"
1511342556041   Marionette  DEBUG   Received DOM event "pagehide" for "about:blank"
1511342556042   Marionette  DEBUG   Received DOM event "unload" for "about:blank"
Unable to read VR Path Registry from C:\Users\abcd\AppData\Local\openvr\openvrpaths.vrpath
JavaScript warning: https://www.example.com:12345/example line 60: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/example line 63: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 95: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 314: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 314: unreachable code after return statement
1511342561264   Marionette  DEBUG   Received DOM event "DOMContentLoaded" for https://www.example.com:12345/example "
1511342561957   Marionette  DEBUG   Received DOM event "pageshow" for "https://www.example.com:12345/example "
1511342563281   Marionette  DEBUG   Received DOM event "beforeunload" for "https://www.example.com:12345/example "
1511342563789   Marionette  DEBUG   Received DOM event "pagehide" for "https://www.example.com:12345/example "
1511342563814   Marionette  DEBUG   Received DOM event "unload" for "https://www.example.com:12345/example "
JavaScript warning: https://www.example.com:12345/example line 60: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/example line 63: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 95: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 314: unreachable code after return statement
JavaScript warning: https://www.example.com:12345/exampleclasses.js?D5153EE28C47, line 314: unreachable code after return statement
1511342565476   Marionette  DEBUG   Received DOM event "DOMContentLoaded" for "https://www.example.com:12345/example "
1511342565703   Marionette  DEBUG   Received DOM event "pageshow" for "https://www.example.com:12345/example "
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //a[@title='Statistics']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\abcd\AppData\Local\Temp\rust_mozprofile.8ZsWdwktZ3Ao, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=XP, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=57.0, platformVersion=10.0, moz:processID=4204, browserName=firefox, javascriptEnabled=true, platformName=XP, moz:webdriverClick=false}]
Session ID: dac831b2-d5ac-4700-b222-5c7a6e5fab82
*** Element info: {Using=xpath, value=//a[@title='Statitics']}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/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:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:458)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at messagemonitor.Forefox.main(Forefox.java:69)

提前谢谢。

2 个答案:

答案 0 :(得分:0)

可能是,你可以尝试使用下面给出的动作类。

WebElement tabStatistics=driver.findElement(By.xpath("//a[@title='Statistics']"));
Actions actions=new Actions(driver);
actions.moveToElement(tabStatistics).click(tabStatistics).perform();

它可能有用。

答案 1 :(得分:0)

您需要使用 JavaScriptExecutor 执行此任务,WebDriver无法单击已禁用或不可见的元素。所以尝试下面的代码可能会帮助你。

JavascriptExecutor js = (JavascriptExecutor) webDriver;
String xpath ="your xpath";
js.executeScript("document.querySelector("+xpath+").click()");