java - Internet Explorer : org. openqa.selenium.NoSuchElementException 的 Selenium 问题:无法使用 xpath 找到元素

标签 java selenium internet-explorer-11 webdriverwait selenium-iedriver

我正在尝试在 Internet Explorer (IE 11) 中为我的系统运行 Selenium(Selenium 独立服务器 3.141.59)。它一直工作到登录。
登录后,它会打开一个新窗口,我使用以下行打开所需的页面。

driver.get("http://appurl/function1.jsp");
之后我找不到任何元素,甚至无法通过 xpath 找到,因为我在下面收到此错误。

org.openqa.selenium.NoSuchElementException : Unable to find element with xpath == //input[@value='Continue']

  @BeforeClass(alwaysRun = true)
  public void setUp() throws Exception {
    DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
    cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS);
    InternetExplorerOptions opt = new InternetExplorerOptions();
    opt.merge(cap);
    System.setProperty("webdriver.ie.driver", "D:\\JavaProjects\\Test\\libs\\IEDriverServer.exe");
    driver = new InternetExplorerDriver(opt);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
有人可以帮我解决这个问题吗?非常感谢您提前。

更新
我按照建议进行了以下更改。
driver.get("http://appurl/function1.jsp");
// after login to system I navigate to the page in above URL
new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Continue']"))).click();
// waiting for the button with value as 'Continue' to be clickable
但我发现没有成功,因为问题仍然存在。
我也在下面添加日志。
RemoteTestNG] detected TestNG version 7.0.1
Started InternetExplorerDriver server (32-bit)
3.9.0.0
Listening on port 16289
Only local connections are allowed
Jan 25, 2021 4:07:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
FAILED: testLogintoImmunizationLive
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //input[@value='Continue'] (tried for 40 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at com.immunization.IMMtestLogin.testLogintoImmunizationLive(IMMtestLogin.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[@value='Continue']
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'IT-PC-X', ip: 'IP_ADDRESS', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
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:16289/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss}
Session ID: 8e60910f-bfeb-4580-92bb-ddd8e7d06c84
*** Element info: {Using=xpath, value=//input[@value='Continue']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:641)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:638)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    ... 29 more


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

最佳答案

可能您需要使用等待功能,直到 continueInput 元素在 DOM 页面上可点击。代码如下;

continueInput = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.XPATH, '//input[@value='Continue']')))

关于java - Internet Explorer : org. openqa.selenium.NoSuchElementException 的 Selenium 问题:无法使用 xpath 找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65286421/

相关文章:

ajax - 您如何使用 Selenium 测试 Ajax 应用程序并使其保持稳定?

javascript - 如何为 ie11 的 String.repeat 函数添加 polyfill?

regex - 输入模式在 IE11 中不完全匹配,但在 Chrome 中,FF

java - 当从 Quartz 调度程序调用函数时,如何将 Http 请求/响应作为参数传递给函数?

java - 使用 TreeMap 和 Comparator 按值对 HashMap 进行排序

java - 声明私有(private) String 数组并初始化数组元素会出现错误

java - 带有用户输入的标记变量以退出

python - Selenium 无法访问Youtube搜索栏?

java - 在 Selenium Java 中选择多个下拉列表

javascript - Window.focus() 在 IE11 中不起作用