【问题标题】:Unable to access elements Inside a shadow Frame using selenium java无法使用 selenium java 访问阴影框架内的元素
【发布时间】:2018-05-21 07:54:42
【问题描述】:

我正在尝试访问位于影子 iframe 内的元素。我可以切换到该框架,但是在尝试访问其中的元素时,出现过时的异常。任何人都可以对此提供帮助,我将不胜感激。提前谢谢..

我用下面的代码

public void enterCustomerDetails() throws InterruptedException {

        Thread.sleep(2000);
        WebElement root1 = sfdcSelFW.driver.findElement(By.tagName("sb-page-container"));
        WebElement shadowRoot1 = expandRootElement(root1);
        WebElement root2 = shadowRoot1.findElement(By.cssSelector("sb-line-editor"));
        WebElement shadowRoot2 = expandRootElement(root2);
        WebElement root3 = shadowRoot2.findElement(By.cssSelector("sb-le-custom-action-services"));
        System.out.println(root3.getAttribute("id"));
        WebElement shadowRoot3 = expandRootElement(root3);
        WebElement root4 = shadowRoot3.findElement(By.cssSelector("span>sb-popup#popup"));
        WebElement shadowRoot4 = expandRootElement(root4);
        WebElement root5 = shadowRoot4.findElement(By.cssSelector("sb-dialog"));
        WebElement shadowRoot5 = expandRootElement(root5);
        sfdcSelFW.driver.switchTo().frame(shadowRoot5.findElement(By.cssSelector("div>div.sbDialog>div#content>iframe")));
        Thread.sleep(10000);
        WebElement saveBtnEle =  shadowRoot5.findElement(By.cssSelector(" div.slds-grid input"));
        saveBtnEle.click();
}

在尝试执行此步骤时切换到框架后此处“WebElement saveBtnEle = shadowRoot5.findElement(By.cssSelector("div.slds-grid input"));” 它抛出异常。

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=60.0.3112.101)
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 92 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'AKKSR01-7470', ip: '10.135.116.222', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.28.455520 (cc17746adff54984afff480136733114c6b3704b), userDataDir=C:\Users\AKKSR01\AppData\Local\Temp\scoped_dir9256_23445}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.101, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: b0870cc9bd01750302e3a69fb388ebbf
*** Element info: {Using=css selector, value= div.slds-grid input}

Below is the sample snippet for the application and the DOM structure

【问题讨论】:

标签: javascript java html css selenium


【解决方案1】:

您指的是shadowRoot5 元素,即使在切换到框架后也能在框架内找到元素。您应该使用sfdcSelFW.driver.findelement 而不是shadowRoot5.findElement,如下所示。

WebElement saveBtnEle =  sfdcSelFW.driver.findElement(By.cssSelector(" div.slds-grid input"));
saveBtnEle.click();

完整代码,

public void enterCustomerDetails() throws InterruptedException {

        Thread.sleep(2000);
        WebElement root1 = sfdcSelFW.driver.findElement(By.tagName("sb-page-container"));
        WebElement shadowRoot1 = expandRootElement(root1);
        WebElement root2 = shadowRoot1.findElement(By.cssSelector("sb-line-editor"));
        WebElement shadowRoot2 = expandRootElement(root2);
        WebElement root3 = shadowRoot2.findElement(By.cssSelector("sb-le-custom-action-services"));
        System.out.println(root3.getAttribute("id"));
        WebElement shadowRoot3 = expandRootElement(root3);
        WebElement root4 = shadowRoot3.findElement(By.cssSelector("span>sb-popup#popup"));
        WebElement shadowRoot4 = expandRootElement(root4);
        WebElement root5 = shadowRoot4.findElement(By.cssSelector("sb-dialog"));
        WebElement shadowRoot5 = expandRootElement(root5);
        WebElement frame =shadowRoot5.findElement(By.cssSelector("div>div.sbDialog>div#content>iframe")
        WebDriverWait wait = new WebDriverWait(sfdcSelFW.driver,30);
        wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame);
        WebElement saveBtnEle =  sfdcSelFW.driver.findElement(By.cssSelector(" div.slds-grid input"));
        saveBtnEle.click();
    }

【讨论】:

  • 我也尝试过这种方式。然后脚本在对其执行操作时抛出异常。即在执行 saveBtnEle.click();在这里操作,异常是“WebDriver Exception”:..org.openqa.selenium.WebDriverException:未知错误:脚本没有返回元素引用
  • 可能是在找到元素之后点击它之前页面正在刷新。
  • 哦。但在那种情况下如何处理
  • 您可以等待帧加载。使用 WebDriverWait 等待 = new WebDriverWait(driver,10); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameName);
  • 感谢 Murthi,但我使用了它,但它也无法正常工作。我使用 WebElement saveBtnEle = sfdcSelFW.driver.findElement(By.cssSelector(" div.slds-grid input" ));是我要查找的元素也在 iframe 所在的同一阴影根下,据我所知,我们只能使用父阴影元素访问阴影元素。我在这里遵循相同的过程
猜你喜欢
  • 2021-01-13
  • 2022-01-03
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多