开发者问题收集

JAVA Selenium iFrame 问题:click() 正在寻找活动框架,但框架在点击方法期间发生了变化

2022-04-07
240

我目前正在测试 PEGA 应用程序的 UI,而我偶然发现了一个问题,即我单击一个按钮,而该按钮又会保存一个项目并关闭框架。单击方法完成后,它仍在寻找 y 帧,但停留在 x 帧上,因此崩溃。

使用时会出现以下问题:

driver.findElement(By.x("selector")).click(); 

框架正在切换,并且在框架中它仍在尝试定位上一个活动框架(此时不可见)。

我在 PegaGadget2Ifr 帧上,但最终在 PegaGadget1Ifr 上进行单击。

这是我正在运行的代码:

    public void saveProjectAsDraft(){
        Project_page pp = new Project_page(FDMPortal.getActiveFrameId(true), testEnv);
        FDMObjectsBean.setProject_page(pp);
        pp.saveDraft();
    }

public Workplace_details_page saveDraft(){
        findElement(SAVE_DRAFT).click();
        return new Workplace_details_page(getActiveFrameId(true),testEnv);
    }

收到此错误消息。

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#PegaGadget2Ifr"}
  (Session info: chrome=99.0.4844.74)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
os.version: '10.0', java.version: '1.8.0_312'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 99.0.4844.74, chrome: {chromedriverVersion: 99.0.4844.51 (d537ec02474b5..., userDataDir: x, goog:chromeOptions: {debuggerAddress: localhost:63949}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: x
*** Element info: {Using=id, value=PegaGadget2Ifr}
    at sun.reflect.GeneratedConstructorAccessor16.newInstance(Unknown Source)
    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.findElementById(RemoteWebDriver.java:372)
    at org.openqa.selenium.By$ById.findElement(By.java:188)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at com.pega.framework.PegaWebDriverImpl.switchToActiveFrame(PegaWebDriverImpl.java:613)
    at com.pega.framework.PegaWebElementImpl.click(PegaWebElementImpl.java:513)
    at com.pega.framework.PegaWebElementImpl.click(PegaWebElementImpl.java:407)
    at com.pega.crm.workobjects.Project_page.saveDraft(Project_page.java:166)
    at stepdefs.WorkplaceStepDefs.saveProjectAsDraft(WorkplaceStepDefs.java:390)```
2个回答

首先切换到元素所在的框架,然后对元素执行活动。 使用其中一种可用方法

switchTo().frame(Index)
switchTo().frame(Web Element)
switchTo().frame(Id or name)
Ajitav Basak
2022-04-07

随着前一个框架 PegaGadget2Ifr 关闭,理想的方法是先切换到 默认内容

driver.switchTo().defaultContent();

然后尝试切换到 PegaGadget1Ifr 框架。

undetected Selenium
2022-04-07