繁体   English   中英

无法在内部的 html dom 和 selenium 中的 object 标记中定位元素

[英]Unable to locate an element in an html dom which is inside and object tag in selenium

I am working on automating test on an html page using selenium in c# where the parent/outer html page has an object tag and in turn inside this object tag we have another html page. 这是示例 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&amp;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>

我需要找到“做某事”和“做其他事情”按钮并单击它。 我曾尝试使用切换帧方法,但没有奏效。 我也尝试过更改 window 手柄,但也没有用。

通过 findelement function,我可以从 go 到 object 标签,但不能超出此范围。 还尝试在 stackoverflow 中搜索解决方案,但找不到相关位。 有人可以帮忙吗。

==================================================== =================

我对此有所了解,因此编辑问题并在此处添加更多信息。 解决方案是这样的。 我通过 id 找到 object 标签。 object 标签的“数据”属性包含内部 html 的 url,所以我获取它。 然后我需要驱动程序导航到这个内部 url。 从那里我可以找到内部 html 内的元素。

var element = driver.FindElement(By.Id("abcd1234"));
string innerurl = element.GetAttribute("data");
driver.Navigate().GoToUrl(innerurl);

However this leads to another problem, previously outer html was opened in browser and only a part of it containing the inner html, but with the above solution, inner html is loaded again directly in browser and not within its parent html. 并且父母的网络元素丢失了。 所以新的问题是,如何绕过它。

您应该可以使用以下内容。 这是 angular 代码,因此您可能需要为 Protractor 添加包装器。 不过,我们总是能够在不使用 Protractor 的情况下找到元素。

FindElement(By.XPath("//button[@class='btn btn-outline']")).Click();
FindElement(By.XPath("//button[@class='btn btn-primary verfy-btn']")).Click();

或者

FindElement(By.XPath("//button[text()='DO SOMETHING']")).Click();
FindElement(By.XPath("//button[text()='SO SOMTHING ELSE']")).Click();

切换到另一个 Window:

    public static string SwitchToPopup()
    {
        var mainHandle = Driver.CurrentWindowHandle;
        var handles = Driver.WindowHandles;

        foreach (var handle in handles)
        {
            if (mainHandle == handle)
            {
                continue;
            }
            Driver.SwitchTo().Window(handle);
            break;
        }
        var result = Url;
        return result;

    }

    public static void GoToMainHandle()
    {
        var handles = Driver.WindowHandles;
        foreach (var handle in handles)
        {
            Driver.SwitchTo().Window(handle);
            break;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM