繁体   English   中英

Selenium 无效 session id 来自当前 webdriver 的循环

[英]Selenium invalid session id from loop with current webdriver

在循环中启动多个 driver.get 命令时出现错误。 I have looked at people with similar problems ie org.openqa.selenium.NoSuchSessionException: no such session error in Selenium automation tests using ChromeDriver Chrome with Java but the problem there was the jdk version, chrome version and such. 我可以为每个 webdriver 实例启动一个 get 命令,所以我不相信这是我的 jdk、chromedriver 或 selenium 的一个版本,这就是问题所在。

String[] referenceID = reference;
        String[] commandID = command;
        String url = "https://"+website;
        System.out.println(Arrays.deepToString(referenceID));
        System.out.println(Arrays.deepToString(commandID));
        System.out.println("Done");
        System.setProperty("webdriver.chrome.driver","D:\\selenium\\libs\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();  
        
        
        long waitTime = 0;
        
        long start = System.nanoTime();
        int times = gui.getTestAmount();
        int i = 0;
        
        boolean reset = false;
        driver.get(url);
        while(times != 0) {
            if(reset) {
                try {
                    Thread.sleep(9000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                driver.get(url);
                reset = false;  
            }
            
            if(commandID[i].equals("Click")) {
                driver.findElement(By.xpath(referenceID[i])).click();
                System.out.println("Clicked");
            }
            if (commandID[i].equals("Text")) {
                System.out.println("testing text");
                String s = referenceID[i];
                String id = s.substring(0, s.indexOf(' '));
                System.out.println(id);
                String content = s.substring(s.indexOf(' ') + 1);
                System.out.println(content);
                driver.findElement(By.xpath(id)).sendKeys(content);
            }
            if (commandID[i].equals("Wait")) {
                try {
                    Thread.sleep(Integer.parseInt(referenceID[i])*1000);
                    waitTime += (Integer.parseInt(referenceID[i])*1000);
                    System.out.println("Waited");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Waited done");
            }
            i++;
            if(i>referenceID.length-1) {
                System.out.println("in reset");
                reset = true;
                driver.close();
                i=0;
                times--;
            }
        }

错误:

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.NoSuchSessionException: invalid session id
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'LAPTOP-E1N87PDH', ip: 'XXXXXXXX(edited out myself)', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.116, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\marti\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:56414}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: ea7286c558f6d6969eb16fcb410e68af
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    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:552)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:277)
    at data.getInfo.print(getInfo.java:39)
    at gui.gui.lambda$5(gui.java:153)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  /**
   * Close the current window, quitting the browser if it's the last window currently open.
   */
  void close();

您不需要在获取之间关闭——每次获取都会重新加载浏览器 window 的内容。

我猜问题是在 if 语句中启动驱动程序。 你可以试试给driver.get(url); 之前while(times != 0)

相信驱动程序在 if 块完成后变得不活动,因此其他元素没有响应。 请参阅此以获取更多解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM