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> </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">
Android
</li>
<li class="blue-hover">
iOS
</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">
Getting Started Guide
</li>
<li class="blue-hover">
API Reference
</li>
<li class="blue-hover">
Encryption
</li>
<li class="blue-hover">
Glossary of Terms
</li>
<li class="blue-hover">
Hackers
</li>
<li class="blue-hover">
Designers
</li>
<li class="blue-hover">
Support
</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">
Desktop/Web MFA
</li>
<li class="blue-hover">
Native Mobile MFA
</li>
<li class="blue-hover">
Authenticator
</li>
</ul>
</li>
<li class="has-dropdown">
<a href="#">Protocols & Plugins
<i class="zmdi zmdi-chevron-down margin-left-qtr"></i>
</a>
<ul class="dropdown">
<li class="blue-hover">
WordPress
</li>
<li class="blue-hover">
Drupal
</li>
<li class="blue-hover">
OAuth
</li>
<li class="blue-hover">
OpenID
</li>
<li class="blue-hover">
SSH PAM
</li>
</ul>
</li>
<li class="blue-hover">
FAQ
</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>
Related
I have used to below code to extract the href content
soup = bs(html, 'html.parser')
pagesize_content = soup.find_all('div',attrs={'class':"pull-right"})
print(pagesize_content)
When I the class "Pull-right"
output as below
[<div class="col-md-4 pull-right">
<button class="btn btn-default closebutton" data-dismiss="modal" type="button">Close</button>
<button class="btn btn-success" id="Name Properties" type="submit">
Submit
<em class="fa fa-check"></em>
</button>
</div>, <div class="pull-right">
<ul class="pagination">
<li class="disabled">
<a class="disabled" href="javascript:void(0)">
First
<em class="fa fa-angle-double-left"></em>
</a>
</li>
<li class="active">
1
</li>
<li class="">
2
</li>
<li class="">
3
</li>
<li class="">
4
</li>
<li class="">
<a href="/Test/country?page=4">
Last
<em class="fa fa-angle-double-right"></em>
</a>
</li>
</ul>
</div>]
From that how to extract the href link.
When I try with
print(pagesize_content.ul.li.a['href'])
error displaying
pagesize_content = soup.find_all('a',attrs={'class':"fa fa-angle-double-right"},href=True)
print(pagesize_content)
empty output
Got myself a bootstrap navbar like this:
<nav class="navbar navbar-expand-lg bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav nav-tabs" >
<li class="nav-item active">
<a class="nav-link" href="{{ url_for('user', username=current_user.username) }}" data-toggle="tab">User</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{{ url_for('home') }}" data-toggle="tab">Home</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto">
{% if current_user.is_anonymous %}
<li class="nav-item active">
<a class="nav-link" href="/login">Login</a>
</li>
{% else %}
<li class="nav-item active">
<a class="nav-link" href="/logout">Logout</a>
</li>
{% endif %}
</ul>
</div>
</nav>
but for some reason when i'm using the "data-toggle=tab" option the tabs start working but they don't redirect me to given urls, when i remove the "data-toggle=tab" urls work fine again
if you need to redirect to a link. No need to use data-toggle="tab". the href in an <a/> tag with data-toggle="tab" attribute means you're using bootstrap tabs which has a js counter part.
I`ve got two tabs:
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="open-tab" data-toggle="tab" href="#open" role="tab" aria-controls="open" aria-selected="true">Active Projects</a>
</li>
<li class="nav-item">
<a class="nav-link" id="close-tab" data-toggle="tab" href="#close" role="tab" aria-controls="close" aria-selected="false">Inactive Projects</a>
</li>
</ul>
The Model of the Application got a field {{app.is_active}}.
Is Filtering the tab with a script is the best option to only show "Active projects" in the active tab and "Inactive projects" in the inactive tab?
Many Thanks,
if you're not using django rest frame work.
it has a simple solution:
<ul class="nav nav-tabs" id="myTab" role="tablist">
{% for item in app_list %}
{% if item.is_active %}
<li class="nav-item active">
<a class="nav-link active" id="open-tab" data-toggle="tab" href="#open" role="tab" aria-controls="open" aria-selected="true">{{ item }}</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" id="close-tab" data-toggle="tab" href="#close" role="tab" aria-controls="close" aria-selected="false">{{ item }}</a>
</li>
{% endif %}
{% endfor %}
</ul>
In advance I apologize for my English, because I write with an interpreter.
There is a task to click on one of these accounts, suppose AAccount, using Selenium Web Driver (Python, Chrome).
The problem is that it's impossible to do this on XPath and CSS, because the elements are the same.
Here is part of the source code for the page. I also attached a screenshot of the page.
</style>
</head>
<body>
<div class="wrapper">
<div class="gb_ag" ng-non-bindable="" id="gb"><div class="gb_hf gb_fg"><div class="gb_ib gb_fg gb_R gb_eg"><div class="gb_7d gb_R gb_fg gb_7f"><div class="gb_Q gb_R gb_S gb_fg"><span class="gb_P gb_R">TeAOK</span></div></div><div class="gb_sc gb_fg gb_R"><div class="gb_ga" id="gbsfw" style="background-color:#eee;min-width:376px"></div><div class="gb_Ac gb_Wc gb_R gb_Bc"><div class="gb_Dc"><a class="gb_b" aria-haspopup="true" role="button" tabindex="0"><div class="gb_Ec gb_Fc gb_4b"></div></a><div class="gb_lb"></div><div class="gb_kb" style="border-bottom-color:#eee"></div></div><div class="gb_ga" aria-hidden="true" aria-live="assertive"></div></div><div class="gb_fb gb_Wc gb_fg gb_R"><div class="gb_Dc gb_hb gb_fg gb_R"><a class="gb_b gb_db gb_R" href="https://accounts.google.com/SignOutOptions?hl=ru&continue=https://accounts.google.com/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline" title="Аккаунт Google: TeAOK G9T3arz
(teaokg9t3arz#gmail.com)" role="button" tabindex="0"><span class="gb_8a gbii"></span></a><div class="gb_lb"></div><div class="gb_kb"></div></div><div class="gb_mb gb_ga" aria-label="Информация об аккаунте" aria-hidden="true"><div class="gb_pb"><a class="gb_qb gb_tf gb_sb" aria-label="Изменить картинку профиля" href="https://profiles.google.com/?hl=ru"><div class="gb_tb gbip" title="Профиль"></div><span class="gb_jb">Изменить</span></a><div class="gb_rb"><div class="gb_ub gb_vb">TeAOK G9T3arz</div><div class="gb_wb">teaokg9t3arz#gmail.com</div><div class="gb_ob">Конфиденциальность</div><a class="gb_Fa gb_qf gbp1 gb_xb" href="https://myaccount.google.com/?utm_source=OGB&utm_medium=act">Мой аккаунт</a></div></div><div class="gb_Cb"><div class="gb_Db gb_6a" aria-hidden="true"><a class="gb_Eb gb_Mb" href="/b/0/DelegateAccountSelector?continue=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Fauth%3Fclient_id%3D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%26redirect_uri%3Dhttps%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php%26scope%3Dhttps%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube%26response_type%3Dcode%26access_type%3Doffline&authuser=0"><img class="gb_Ob gb_sb" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" alt="Профиль" data-src="https://lh3.googleusercontent.com/-Yg4816hNGF4/AAAAAAAAAAI/AAAAAAAAAAA/AAyYBF6hOAK-q02pBDqts2Bde8yxxNX1wQ/s48-c-mo/photo.jpg"><div class="gb_Hb"><div class="gb_Pb">TeAOK G9T3arz</div><div class="gb_Qb">teaokg9t3arz#gmail.com (по умолчанию)</div></div></a></div><a class="gb_Sb gb_6a" href="https://myaccount.google.com/brandaccounts?authuser=0&continue=https://accounts.google.com/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline&service=/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline%26authuser%3D%24authuser" aria-hidden="true"><span class="gb_Tb gb_4b"></span><div class="gb_Ub">Все аккаунты брендов »</div></a></div><div class="gb_yb"><div><a class="gb_Fa gb_pf gb_xb" href="https://accounts.google.com/AddSession?continue=https%3A%2F%2Faccounts.google.com%2Fb%2F0%2FDelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline">Добавить аккаунт</a></div><div><a class="gb_Fa gb_rf gb_yf gb_xb" id="gb_71" href="https://accounts.google.com/Logout" target="_top">Выйти</a></div></div></div></div></div></div><div class="gb_Zb gb_R gb_3b" id="gbq1" style="max-width:152px;min-width:152px"><div class="gb_0b"><a class="gb_3d gb_2b" href="/" title="Мой аккаунт" data-ved="0EMQuCAQ"><span class="gb_Za gb_4b"></span></a></div></div></div><div id="gbw"></div></div><div class="gb_sd" id="gba"></div>
<div class="delegate-account-selector main content clearfix">
<div class="sign-in">
<div class="signin-box">
<h2 id="signin-action">Выберите аккаунт</h2>
<ol class="accounts" id="account-list">
<li id="account-teaokg9t3arz#gmail.com">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=none">
<span class="account-image" id="account-image-0"></span>
<span class="account-name">TeAOK G9T3arz</span>
<span class="account-email" id="account-email-0">teaokg9t3arz#gmail.com</span>
<span class="select-account text-icon-select" id="account-account-0">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=105773503169388790046">
<span class="account-image" id="account-image-1"></span>
<span class="account-name">EAccount</span>
<span class="select-account text-icon-select" id="account-account-1">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=116942572818460775716">
<span class="account-image" id="account-image-2"></span>
<span class="account-name">FAccount</span>
<span class="select-account text-icon-select" id="account-account-2">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=110025695516281123129">
<span class="account-image" id="account-image-3"></span>
<span class="account-name">GAccount</span>
<span class="select-account text-icon-select" id="account-account-3">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=100327913140968503320">
<span class="account-image" id="account-image-4"></span>
<span class="account-name">CAccount</span>
<span class="select-account text-icon-select" id="account-account-4">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=114686295257876098243">
<span class="account-image" id="account-image-5"></span>
<span class="account-name">DAccount</span>
<span class="select-account text-icon-select" id="account-account-5">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=101169419531313874972">
<span class="account-image" id="account-image-6"></span>
<span class="account-name">iAccount</span>
<span class="select-account text-icon-select" id="account-account-6">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=116191438938972075200">
<span class="account-image" id="account-image-7"></span>
<span class="account-name">AAccount</span>
<span class="select-account text-icon-select" id="account-account-7">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=106861284959818641724">
<span class="account-image" id="account-image-8"></span>
<span class="account-name">BAccount</span>
<span class="select-account text-icon-select" id="account-account-8">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=104823728391082992328">
<span class="account-image" id="account-image-9"></span>
<span class="account-name">IAccount</span>
<span class="select-account text-icon-select" id="account-account-9">Выберите</span>
</a>
</li>
<li id="account-">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&response_type=code&access_type=offline&pageId=102918689899435112584">
<span class="account-image" id="account-image-10"></span>
<span class="account-name">JAccount</span>
<span class="select-account text-icon-select" id="account-account-10">Выберите</span>
</a>
</li>
</ol>
</div>
</div>
<div class="side-content">
<div class="product-headers">
<h1 class="redtext">Аккаунты</h1>
</div>
<div class="select-account">
<p>
Выберите аккаунт Google
</p>
<ul>
<li>
TeAOK G9T3arz
(teaokg9t3arz#gmail.com)
</li>
<li>
EAccount
</li>
<li>
FAccount
</li>
<li>
GAccount
</li>
<li>
CAccount
</li>
<li>
DAccount
</li>
<li>
iAccount
</li>
<li>
AAccount
</li>
<li>
BAccount
</li>
<li>
IAccount
</li>
<li>
JAccount
</li>
</ul>
</div>
</div>
</div>
<div class="google-footer-bar">
<div class="footer content clearfix">
<ul id="footer-list">
<li>Google</li>
<li>
<a href="https://accounts.google.com/TOS?loc=RU&hl=ru" >Конфиденциальность и условия</a>
</li>
<li id="footer-help"><a href="https://support.google.com/accounts?hl=ru" >Справка</a></li>
</ul>
</div>
</div>
<script type="text/javascript">
You can try to locate required element by partial link text:
driver.find_element_by_partial_link_text("AAccount").click()
If it doesn't work, you can try to add some time to wait for required element:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait(driver, 10).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "AAccount"))).click()
For the following html:
<ul id="dataset-menu" class="treeview">
<li id="cat_01" class="expandable"></li>
<li id="cat_02" class="collapsable">
<div class="hitarea collapsable-hitarea"></div>
<span class="folder" title=""></span>
<ul style="display: block;">
<li></li>
<li>
<span class="collection">
<div class="cell">
<input id="coll_5555" class="dataset_checkbox" type="checkbox" name="dataset_checkbox" value="5555"></input>
</div>
<div class="cell"></div>
</span>
</li>
<li class="last"></li>
</ul>
</li>
<li id="cat_03" class="expandable"></li>
I have to select the item where the following occurs:
<input id="coll_5555" class="dataset_checkbox" type="checkbox" name="dataset_checkbox" value="5555"></input>
Any idea please?
As i understand first you have to click on li with id cat_02 and then click on the checkbox.
driver.find_element_by_css_selector("#cat_02 div.collapsable-hitarea").click()
driver.find_element_by_id("coll_5555").click();