Python-Behave Selenium - 从父元素获取所有子锚文本

时间:2017-11-13 01:27:39

标签: python selenium python-behave

我有以下标题,我试图从中获取多个文本。我只能获取菜单Mobile App GuideQuick LinksSDKsProtocols & PluginsFAQ的文字。但是我无法获得子菜单文本。

我使用elem.find_elements_by_css_selector("*")elem.find_elements_by_xpath(".//*")创建了header标记下所有子元素的列表。

我认为这是因为我只是迭代到if len(child.text) > 0父元素的最近孩子。任何人都知道我如何遍历标题标记的所有子元素(包括子菜单)?

from behave import given, when, then
from selenium.webdriver.common.keys import Keys

# @given('the user is on documentation home page')
# def on_home_page(context):
#     context.driver.get("URL removed")

@when('the header is displayed')
def header_display(context):
    assert context.driver.find_element_by_xpath('/html/body/header')

@then('the header should have {menu}')
def header_menu(context, menu):
    elem = context.driver.find_element_by_tag_name("header")
    all_children_by_css = elem.find_elements_by_css_selector("*")
    all_children_by_xpath = elem.find_elements_by_xpath(".//*")

    children = []
    for child in all_children_by_xpath:
        # print(len(child.text))
        if len(child.text) > 0:
            # print(child.text)
            if child.text not in children:
                children.append(child.text)

    print(children)
    if menu in children:
        assert True
    else:
        assert False

页眉的源代码

<header class="header docs-header">
    <nav class="top-bar" data-topbar role="navigation">
        <ul class="title-area">
            <li class="name">
                <h1>
                    <a title="LaunchKey Home Page" class="home-link" href="https://www.iovation.com/launchkey">
                        <img alt="LaunchKey Logo" class="header-logo" src="_static/images/logo-name.svg">
                    </a>
                </h1>
            </li>
            <li class="toggle-topbar menu-icon">
                <a href="#" title="menu-toggle">
                    <span>&nbsp;</span>
                </a>
            </li>
        </ul>
        <section class="top-bar-section">
            <ul class="left">

                <li class="has-dropdown">
                    <a href="#">Mobile App Guide
                        <i class="zmdi zmdi-chevron-down margin-left-qtr"></i>
                    </a>
                    <ul class="dropdown">
                        <li class="blue-hover">
                            <a href="user/mobile-app-guide/index.html">Android</a>
                        </li>
                        <li class="blue-hover">
                            <a href="user/mobile-app-guide/index.html">iOS</a>
                        </li>

                    </ul>
                </li>
                <li class="has-dropdown">
                    <a href="#">Quick Links
                        <i class="zmdi zmdi-chevron-down margin-left-qtr"></i>
                    </a>
                    <ul class="dropdown">
                        <li class="blue-hover">
                            <a href="common/getting-started-guide.html">Getting Started Guide</a>
                        </li>
                        <li class="blue-hover">
                            <a href="developer/api/index.html">API Reference</a>
                        </li>
                        <li class="blue-hover">
                            <a href="developer/encryption/index.html">Encryption</a>
                        </li>
                        <li class="blue-hover">
                            <a href="glossary.html">Glossary of Terms</a>
                        </li>
                        <li class="blue-hover">
                            <a href="hacker/index.html">Hackers</a>
                        </li>
                        <li class="blue-hover">
                            <a href="common/resources/index.html">Designers</a>
                        </li>
                        <li class="blue-hover">
                            <a href="https://launchkey.com/support" target="_blank">Support</a>
                        </li>
                    </ul>
                </li>
                <li class="has-dropdown">
                    <a href="#">SDKs
                        <i class="zmdi zmdi-chevron-down margin-left-qtr"></i>
                    </a>
                    <ul class="dropdown">
                        <li class="blue-hover">
                            <a href="developer/web-desktop/index.html">Desktop/Web MFA</a>
                        </li>
                        <li class="blue-hover">
                            <a href="developer/mobile/index.html">Native Mobile MFA</a>
                        </li>
                        <li class="blue-hover">
                            <a href="developer/white-label/index.html">Authenticator</a>
                        </li>
                    </ul>
                </li>
                <li class="has-dropdown">
                    <a href="#">Protocols &amp; Plugins
                        <i class="zmdi zmdi-chevron-down margin-left-qtr"></i>
                    </a>
                    <ul class="dropdown">
                        <li class="blue-hover">
                            <a href="developer/cms/word-press/index.html">WordPress</a>
                        </li>
                        <li class="blue-hover">
                            <a href="developer/cms/drupal/index.html">Drupal</a>
                        </li>
                        <li class="blue-hover">
                            <a href="integrator/protocol/oauth/index.html">OAuth</a>
                        </li>
                        <li class="blue-hover">
                            <a href="integrator/protocol/openid/index.html">OpenID</a>
                        </li>
                        <li class="blue-hover">
                            <a href="integrator/module/pam/index.html">SSH PAM</a>
                        </li>
                    </ul>
                </li>
                <li class="blue-hover">
                    <a href="faq/index.html">FAQ</a>
                </li>
            </ul>
            <ul class="right">
                <li>
                    <div role="search">
                        <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
                            <input type="text" name="q" placeholder="Search docs" tabindex="1" />
                            <input type="hidden" name="check_keywords" value="yes" />
                            <input type="hidden" name="area" value="default" />
                        </form>
                    </div>
                </li>
            </ul>
        </section>
    </nav>
</header>

0 个答案:

没有答案