我正在尝试访问阴影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
答案 0 :(得分:0)
即使在切换到帧之后,您指的是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();
}