org.openqa.selenium.NoSuchElementException 在 IE 中出错,但相同的代码在 Chrome 和 Firefox 中工作正常
org.openqa.selenium.NoSuchElementException error in IE but the same code works fine in Chrome and Firefox
我写了一个登录脚本,当我使用 ChromeDirver 和 FFDriver 执行它时,它工作正常。但是当我 运行 同样使用 IE 驱动程序时,它失败并给出以下错误。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'LENOVO', ip: '192.168.1.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:39714/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: af1a703a-0216-4c67-8c51-1292d13e399c
*** Element info: {Using=id, value=mod-login-username}
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: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:543)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:363)
at org.openqa.selenium.By$ById.findElement(By.java:188)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
at pageObjects.Admin_Login.txtbx_Username(Admin_Login.java:13)
at testcases.testcase01.main(testcase01.java:29)
这是脚本:-
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Admin_Login {
private static WebElement element = null;
public static WebElement txtbx_Username(WebDriver driver) {
element = driver.findElement(By.id("mod-login-username"));
return element;
}
public static WebElement txtbx_Password (WebDriver driver) {
element = driver.findElement(By.xpath("mod-login-password"));
return element;
}
public static WebElement btn_Login (WebDriver driver) {
element = driver.findElement(By.id("mod-login-password"));
return element;
}
}
我不明白为什么脚本显示 "Unable to find element with css selector ..." 错误,因为我只使用 id 来查找元素而不是 CSS 选择器。有没有人可以指点一下。
这个错误信息...
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username
...表示 InternetExplorerDriver 无法根据您使用的 Locator Strategy 定位任何元素。
原因
正如您提到的,使用 ChromeDirver/Chrome 和 GeckoDriver/Firefox 可以使用相同的代码,值得一提的是,不同的 浏览器引擎 呈现 HTML DOM 不同。如此脆弱 定位器策略 可能不适用于所有浏览器。
根据您的问题,script showing the error of "Unable to find element with css selector ..." as I have only used id
根据 WebDriver W3C 编辑草案再次值得一提的是,首选 Locator Strategies 入伍如下:
"css selector"
: CSS 选择器
"link text"
: Link 文本选择器
"partial link text"
: 部分 link 文本选择器
"tag name"
: 标记名称
"xpath"
: XPath 选择器
快照:
通过相应的 客户端 特定绑定传播了更改。对于 Selenium-Java
客户,这里是 client code,其中 Locator Strategies 内部基于 id
和 name
内部转换 等价于 css selector 通过 switchcase 如下:
switch (using) {
case "class name":
toReturn.put("using", "css selector");
toReturn.put("value", "." + cssEscape(value));
break;
case "id":
toReturn.put("using", "css selector");
toReturn.put("value", "#" + cssEscape(value));
break;
case "link text":
// Do nothing
break;
case "name":
toReturn.put("using", "css selector");
toReturn.put("value", "*[name='" + value + "']");
break;
case "partial link text":
// Do nothing
break;
case "tag name":
toReturn.put("using", "css selector");
toReturn.put("value", cssEscape(value));
break;
case "xpath":
// Do nothing
break;
}
return toReturn;
快照:
因此尽管您提供:
(By.id("mod-login-username"))
错误显示:
Unable to find element with css selector == #mod\-login\-username
我写了一个登录脚本,当我使用 ChromeDirver 和 FFDriver 执行它时,它工作正常。但是当我 运行 同样使用 IE 驱动程序时,它失败并给出以下错误。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'LENOVO', ip: '192.168.1.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:39714/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: af1a703a-0216-4c67-8c51-1292d13e399c
*** Element info: {Using=id, value=mod-login-username}
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: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:543)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:363)
at org.openqa.selenium.By$ById.findElement(By.java:188)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
at pageObjects.Admin_Login.txtbx_Username(Admin_Login.java:13)
at testcases.testcase01.main(testcase01.java:29)
这是脚本:-
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Admin_Login {
private static WebElement element = null;
public static WebElement txtbx_Username(WebDriver driver) {
element = driver.findElement(By.id("mod-login-username"));
return element;
}
public static WebElement txtbx_Password (WebDriver driver) {
element = driver.findElement(By.xpath("mod-login-password"));
return element;
}
public static WebElement btn_Login (WebDriver driver) {
element = driver.findElement(By.id("mod-login-password"));
return element;
}
}
我不明白为什么脚本显示 "Unable to find element with css selector ..." 错误,因为我只使用 id 来查找元素而不是 CSS 选择器。有没有人可以指点一下。
这个错误信息...
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username
...表示 InternetExplorerDriver 无法根据您使用的 Locator Strategy 定位任何元素。
原因
正如您提到的,使用 ChromeDirver/Chrome 和 GeckoDriver/Firefox 可以使用相同的代码,值得一提的是,不同的 浏览器引擎 呈现 HTML DOM 不同。如此脆弱 定位器策略 可能不适用于所有浏览器。
根据您的问题,script showing the error of "Unable to find element with css selector ..." as I have only used id
根据 WebDriver W3C 编辑草案再次值得一提的是,首选 Locator Strategies 入伍如下:
"css selector"
: CSS 选择器"link text"
: Link 文本选择器"partial link text"
: 部分 link 文本选择器"tag name"
: 标记名称"xpath"
: XPath 选择器
快照:
通过相应的 客户端 特定绑定传播了更改。对于 Selenium-Java
客户,这里是 client code,其中 Locator Strategies 内部基于 id
和 name
内部转换 等价于 css selector 通过 switchcase 如下:
switch (using) {
case "class name":
toReturn.put("using", "css selector");
toReturn.put("value", "." + cssEscape(value));
break;
case "id":
toReturn.put("using", "css selector");
toReturn.put("value", "#" + cssEscape(value));
break;
case "link text":
// Do nothing
break;
case "name":
toReturn.put("using", "css selector");
toReturn.put("value", "*[name='" + value + "']");
break;
case "partial link text":
// Do nothing
break;
case "tag name":
toReturn.put("using", "css selector");
toReturn.put("value", cssEscape(value));
break;
case "xpath":
// Do nothing
break;
}
return toReturn;
快照:
因此尽管您提供:
(By.id("mod-login-username"))
错误显示:
Unable to find element with css selector == #mod\-login\-username