【发布时间】:2020-06-11 17:16:19
【问题描述】:
我正在使用 c# 中的 selenium 对 html 页面进行自动化测试,其中父/外部 html 页面有一个对象标签,而在这个对象标签内,我们有另一个 html 页面。这是示例 html 的外观。
<!DOCTYPE html>
<html>
<head></head><body id="divAjaxParent">
<div id="_ctl0_BodyContent_divObject" class="object-loading" style="height: 100%; overflow: hidden; padding-bottom: 60%; position: relative; width: 100%;">
<object id="abcd1234" style="height: 100%; left: 0; position: absolute; top: 0; width: 100%;" data="abcd.ashx?action=dosimething&sessionguid=1234567890">
#document == $0
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<title>Tool title</title>
<base href="/deploy/">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<body class="no-scrolling">
<app-root _nghost-c0="" ng-version="7.1.4">
<div _ngcontent-c0=""><div _ngcontent-c0="" class="route-placeholder clearfix">
<app-dnl-dummyapp _nghost-c1="" class="ng-star-inserted">
<clr-modal _ngcontent-c1="" _nghost-c3="" class="ng-tns-c3-0 open">
<button _ngcontent-c3="" class="close ng-tns-c3-0 ng-star-inserted" type="button" aria-label="Close" style="">
<clr-icon _ngcontent-c3="" class="ng-tns-c3-0" shape="close" role="none">
<svg version="1.1" viewBox="0 0 36 36" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" focusable="false" role="img">
<path class="clr-i-outline clr-i-outline-path-1" d="sdfggtrrr"></path></svg></clr-icon></button>
<div _ngcontent-c3="" class="modal-title-wrapper" tabindex="0" id="clr-id-0"><div _ngcontent-c1="" class="modal-title font-28-regular">What do you want to do?</div></div></div>
<div _ngcontent-c1="" class="modal-body" tabindex="0"><p _ngcontent-c1="">Description about the button options</p></div><div _ngcontent-c1="" class="modal-footer">
<button _ngcontent-c1="" class="btn btn-outline" type="button">DO SOMETHING</button>
<button _ngcontent-c1="" class="btn btn-primary verfy-btn" type="button">SO SOMTHING ELSE</button></div></div></div>
<div _ngcontent-c3="" class="clr-sr-only">End of Modal Content</div></div>
<div _ngcontent-c3="" class="modal-backdrop ng-trigger ng-trigger-fade" aria-hidden="true"></div></div></clr-modal>
</app-dnl-dummyapp></div><a _ngcontent-c0="" class="invisible" href=""></a><div _ngcontent-c0="" appsizewatcher="" id="dummy_12345"></div></div></app-root>
</body></html>
</object>
</div></section></body></html>
我需要找到“做某事”和“做其他事情”按钮并单击它。 我尝试过使用切换帧方法,但它没有用。 我也尝试过更改窗口句柄,但也没有用。
通过 findelement 函数,我可以上到对象标签,但不能超越。还尝试在 stackoverflow 中搜索解决方案,但找不到相关位。有人可以帮忙吗。
================================================ ====================
我对此有所了解,因此编辑问题并在此处添加更多信息。解决方案是这样的。我通过 id 定位对象标签。 object 标签的 'data' 属性包含内部 html 的 url,所以我获取它。然后我需要驱动程序导航到这个内部 url。从那里我可以找到内部 html 中的元素。
var element = driver.FindElement(By.Id("abcd1234"));
string innerurl = element.GetAttribute("data");
driver.Navigate().GoToUrl(innerurl);
但是这会导致另一个问题,之前在浏览器中打开了外部 html,其中只有一部分包含内部 html,但是通过上述解决方案,内部 html 再次直接加载到浏览器中,而不是在其父 html 中。并且父母的网络元素丢失了。所以新的问题是,如何绕过它。
【问题讨论】: