java - 来自当前 webdriver 的循环中的 Selenium 无效会话 ID
问题描述
在循环中启动多个 driver.get 命令时出现错误。我看过有类似问题的人,例如 org.openqa.selenium.NoSuchSessionException: no such session error in Selenium automation testing using ChromeDriver Chrome with Java但问题在于 jdk 版本、chrome 版本等。我可以为每个 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();
您不需要在两次获取之间关闭——每次获取都会重新加载浏览器窗口的内容。
推荐阅读
- c# - 如何更新已从 BOT 发送给用户的自适应卡?
- mysql - 如何为一条记录指定多个 id
- css - 如何使用 CSS 在文本区域中放置长标签?
- java - 设置最大消费者数量 jms solace
- square - 应用内支付,是否可以在 Android 应用中保存用户的信用卡列表
- c# - 如何使用 Audit.Net 扩展 Audit.Mvc 设置 AuditEvent 的目标对象
- python-2.7 - --py-files 如何在 pyspark 内部工作
- python - Plotly 无法正确显示谷歌合作笔记本中的图形标题
- c# - 我想使用 FPSController 在场景中创建设置屏幕,但我无法点击编辑器
- javascript - 使用 CSS/HTML 创建类似轮播的表单