IE中的org.openqa.selenium.NoSuchElementException错误,但在Chrome和Firefox中同样的代码可以正常工作

IE中的org.openqa.selenium.NoSuchElementException错误,但在Chrome和Firefox中同样的代码可以正常工作,selenium,selenium-webdriver,selenium-chromedriver,geckodriver,iedriverserver,Selenium,Selenium Webdriver,Selenium Chromedriver,Geckodriver,Iedriverserver,我已经编写了一个登录脚本,当我使用ChromeServer和FFDriver执行它时,它工作得很好。但是,当我使用IE驱动程序运行相同的程序时,它失败了,并给出以下错误 Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username For documentation on this er

我已经编写了一个登录脚本,当我使用ChromeServer和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;
}
}
我不明白为什么脚本显示“无法使用css选择器找到元素…”的错误,因为我只使用id来查找元素,而没有使用css选择器。请任何人提供建议。

此错误消息

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #mod\-login\-username 
…意味着InternetExplorerDriver无法按照您使用的定位器策略定位任何元素

理由 正如您提到的,使用ChromeServer/Chrome和GeckoDriver/Firefox的代码是相同的,值得一提的是,不同的浏览器引擎呈现的结果不同。因此,脆弱的定位策略可能不会在所有浏览器中都有效


根据您的问题,
脚本显示“无法使用css选择器找到元素…”的错误,因为我只使用了id
,再次值得一提的是,根据WebDriver W3C编辑器的草稿首选登记如下:

  • “css选择器”
    :css选择器
  • “链接文本”
    :链接文本选择器
  • “部分链接文本”
    :部分链接文本选择器
  • “标记名”
    :标记名
  • “xpath”
    :xpath选择器
快照:

更改通过相应的客户端特定绑定传播。对于
Selenium Java
客户端,这里是基于
id
的定位器策略通过开关盒在内部转换为等效的css选择器的地方,如下所示:

开关(使用){
案例“类名”:
toReturn.put(“使用”、“css选择器”);
toReturn.put(“值”、“+cssEscape(值));
打破
案例“id”:
toReturn.put(“使用”、“css选择器”);
toReturn.put(“值”、“#”+cssEscape(值));
打破
案例“链接文本”:
//无所事事
打破
案例“名称”:
toReturn.put(“使用”、“css选择器”);
toReturn.put(“value”、“*[name=”+value+“]”);
打破
案例“部分链接文本”:
//无所事事
打破
案例“标记名”:
toReturn.put(“使用”、“css选择器”);
toReturn.put(“价值”,cssEscape(价值));
打破
案例“xpath”:
//无所事事
打破
}

回归回归你能添加相关的HTML吗?用相关的HTML库更新问题!但是,我正在尝试执行交叉浏览器测试。那么,我应该如何处理这样的场景,正如您所解释的,浏览器引擎以不同的方式呈现HTMLDOM。有办法解决这个问题吗?如何编写测试,以便在所有浏览器中都能接受。提前谢谢@DebanjanB@Kaustubh如果您的定位策略得到优化,将不会有任何偏差(显著差异)。是的!是的对不起,但你能帮我一下吗?或者给我一个开始的想法——如何优化定位器策略。提前谢谢@DebanjanB@Kaustubh虽然我的意见将基于专业知识和经验,但尝试使用
属性作为强制措施,再加上
id
名称
,等等,只使用
标记名
。太好了!理解。谢谢你的帮助。