0

I have the following header that I'm trying to get the multiple text from. I'm only able to get the text of menus Mobile App Guide, Quick Links, SDKs, Protocols & Plugins, and FAQ. But I'm not able to get the sub menu text.

I'm using elem.find_elements_by_css_selector("*") and elem.find_elements_by_xpath(".//*") which creates a list of all child elements under header tag.

I think its because I'm only iterating through to closest child of the parent element where if len(child.text) > 0. Anyone know how I can iterate through all the child elements (including sub menu) of the header tag?

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

Source code of page header

<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>
j7an
  • 104
  • 2
  • 11
  • `'the header should have {menu}'` - this step is checking whether only ONE option is in the menu, so why iterate the whole menu ? Just check if this one option exists or not, for example for option `WordPress` --> `find_element_by_xpath( "//header//li[ */text() = 'WordPress' ]")` – krokodilko Nov 13 '17 at 05:24
  • @krokodilko I want to validate each menu individually so that if a menu is missing or different, the specific scenario will fail with the name of the menu failing. Is there a way to match a variable in your find by xpath? like `find_element_by_xpath( "//header//li[ */text() = menu ]")` I get a traceback error. – j7an Nov 13 '17 at 06:02

0 Answers0