IE中的org.openqa.selenium.NoSuchElementException错误,但相同的代码在Chrome和Firefox中可以正常工作 [英] org.openqa.selenium.NoSuchElementException error in IE but the same code works fine in Chrome and Firefox
问题描述
我已经编写了一个登录脚本,当我使用ChromeDirver和FFDriver执行登录脚本时,它可以正常运行.但是,当我使用IE驱动程序运行相同文件时,它失败并给出以下错误.
线程主"中的异常org.openqa.selenium.NoSuchElementException:无法使用CSS选择器找到元素== #mod \ -login \ -username有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html构建信息:版本:'3.12.0',修订版本:'7c6e0b3',时间:'2018-05-08T15:15:08.936Z'系统信息:主机:'LENOVO',ip:'192.168.1.101',操作系统名称:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_171'驱动程序信息:org.openqa.selenium.ie.InternetExplorerDriver功能{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,即.browserCommandLineSwitches :,即.ensureCleanSession:false,即.fileUploadDialogTimeout:3000,即.forceCreateProcessApi:false,ignoreProtectedModeSettings:false,ignoreZoomSetting:false,initialBrowserUrl:http://localhost:39714/,nativeEvents:true,requireWindowFocus:false},setWindowRect:true,超时:{隐式:0,pageLoad:300000,脚本:30000}}会话ID:af1a703a-0216-4c67-8c51-1292d13e399c***元素信息:{Using = id,value = mod-login-username}在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)在java.lang.reflect.Constructor.newInstance(未知来源)在org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)在org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:363)在org.openqa.selenium.By $ ById.findElement(By.java:188)在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)在pageObjects.Admin_Login.txtbx_Username(Admin_Login.java:13)在testcases.testcase01.main(testcase01.java:29)
以下是脚本:-
包pageObjects;导入org.openqa.selenium.By;导入org.openqa.selenium.WebDriver;导入org.openqa.selenium.WebElement;公共类Admin_Login {私有静态WebElement元素= null;公共静态WebElement txtbx_Username(WebDriver驱动程序){元素= driver.findElement(By.id("mod-login-username"));返回元素;}公共静态WebElement txtbx_Password(WebDriver驱动程序){元素= driver.findElement(By.xpath("mod-login-password"));返回元素;}公共静态WebElement btn_Login(WebDriver驱动程序){元素= driver.findElement(By.id("mod-login-password"));返回元素;}}
我不明白为什么脚本显示无法使用css选择器找到元素..."错误,因为我仅使用id来查找元素,而没有使用CSS选择器.任何人都可以请教.
此错误消息...
线程主"中的异常org.openqa.selenium.NoSuchElementException:无法使用CSS选择器找到元素== #mod \ -login \ -username
...表示根据您所使用的 Locator Strategy , InternetExplorerDriver 无法找到任何元素.
原因
正如您提到的,相同的代码可以在ChromeDirver/Chrome和GeckoDriver/Firefox中使用,值得一提的是,不同的 Browser Engine 呈现了
更改通过相应的客户端特定绑定传播.对于 Selenium-Java
客户端,这里是
因此,尽管您提供:
(By.id("mod-login-username"))
错误显示:
无法使用CSS选择器找到元素== #mod \ -login \ -username
I have written a login script and when I execute it using ChromeDirver and FFDriver it works fine. But when I run the same using IE Driver it fails and giving the below error.
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)
Here is the script:-
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;
}
}
I'm not getting why the script showing the error of "Unable to find element with css selector ..." as I have only used id to find the element and not CSS selector. Could anyone please advise.
This error message...
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username
...implies that the InternetExplorerDriver was unable to locate any element as per the Locator Strategy you have used.
Reason
As you have mentioned the same code works using ChromeDirver/Chrome and GeckoDriver/Firefox, it is worth to mention that different Browser Engine renders the HTML DOM differently. So brittle Locator Strategies may not work across all the browsers.
As per your question, script showing the error of "Unable to find element with css selector ..." as I have only used id
it is again worth to mention as per WebDriver W3C Editor's Draft the preferred Locator Strategies enlisted are as follows :
"css selector"
: CSS selector"link text"
: Link text selector"partial link text"
: Partial link text selector"tag name"
: Tag name"xpath"
: XPath selector
Snapshot :
A change was propagated through the respective client specific bindings. For the Selenium-Java
clients here is the client code where internally the Locator Strategies based on id
, and name
are internally converted to equivalent css selector through a switchcase as follows :
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;
Snapshot :
Hence though you provide:
(By.id("mod-login-username"))
Error shows:
Unable to find element with css selector == #mod\-login\-username
这篇关于IE中的org.openqa.selenium.NoSuchElementException错误,但相同的代码在Chrome和Firefox中可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!