java - Selenium 无法在 iframe 中定位元素

标签 java selenium iframe

我可以使用 driver.switchTo().frame("sysDialogLoader"); 从主页切换到 iframe;
我能够发送 key 并执行其中的所有操作

driver.findElement(By.cssSelector("#name")).sendKeys("Krishna");
driver.findElement(By.cssSelector("#timezone")).click();
driver.findElement(By.cssSelector("#timezone > option:nth-child(172)")).click();
driver.findElement(By.cssSelector("#potential")).click();
driver.findElement(By.cssSelector("#potential > option:nth-child(4)")).click();

但是当我尝试最后单击 iframe 上的“确定”按钮时,它会抛出错误

driver.findElement(By.cssSelector("#sysDialogBtns")).click();

完全错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #sysDialogBtns
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:42:22.379Z'
System info: host: 'KKATAMANENI-DT', ip: '10.19.68.134', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 58.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 9280, moz:profile: C:\Users\krishna.katamaneni..., moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 4930cd71-631b-47b3-bc6b-cf356be6a756
*** Element info: {Using=css selector, value=#sysDialogBtns}
    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:160)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:465)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at Login.login_chrome.main(login_chrome.java:40)

HTML:

<div id="sysDialogBtns" name="sysDialogBtns">
  <div class="x-window-bl">
    <div class="x-window-br">
      <div class="x-window-bc">
        <div class="x-window-footer">
          <div class="x-panel-btns-ct">
            <div class="x-panel-btns x-panel-btns-right sysDialogButtons" style="text-align:right">
              <input id="sysDialogWizCancel" name="sysDialogWizCancel" class="BSButtonOS " onclick="dlgCancel()" value="Close" style="display: none;" type="button">
              <input id="sysDialogWizPrevious" name="sysDialogWizPrevious" class="BSButtonOS " onclick="dlgPrevious()" value="< Previous" style="display: none;" type="button">
              <input id="sysDialogWizNext" name="sysDialogWizNext" class="BSButtonOS " onclick="dlgNext()" value="&nbsp;&nbsp;&nbsp;&nbsp;Next >&nbsp;&nbsp;&nbsp;&nbsp;" style="display: none;" type="button">
              <input id="sysDialogBack" name="sysDialogBack" class="BSButtonOS " onclick="dlgGoBack()" value="Go Back" style="display: none;" type="button">&nbsp;&nbsp;
              <input id="sysDialogOK" name="sysDialogOK" class="BSButtonOS " onclick="dlgOK()" value="OK" style="width: 100px; display: inline;" type="button">&nbsp;&nbsp;
              <input id="sysDialogCancel" name="sysDialogCancel" class="BSButtonOS " onclick="dlgCancel()" value="Cancel" style="width: 100px; display: inline;" type="button">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

最佳答案

尝试切换回默认内容然后执行.click()

driver.switchTo().defaultContent();

关于java - Selenium 无法在 iframe 中定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48712299/

相关文章:

python - Selenium 使用 .find 方法返回空列表,即使有项目

python - 元素不可点击,因为另一个元素遮挡了它

testing - Jenkins 上的 TestNG 结果组

Phonegap 上的 Android Webview

用于 XLSX 文件扩展名的 Java 正则表达式

java - [HELP]BST 递归插入时 Java StackOverflow 错误

java - Maven 发布 :prepare namespace prefix "xsi" collides with an additional namespace declared by the element

javascript - IE8中访问iframe Object.prototype

c# - 尝试在 ActionResult 中返回 HTML 会导致 HTTP 406 错误

java - 程序崩溃。破译错误信息