How to use following_sibling method in Selene

Best Python code snippet using selene_python

__init__.py

Source:__init__.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4.. _estatnet__init__.py5Initialisation file.6**About**7**Usage**8"""9# *credits*: `gjacopo <jacopo.grazzini@ec.europa.eu>`_ 10# *since*: Mon Dec 18 17:28:29 201711__all__ = ['spiders']#analysis:ignore12#==============================================================================13# PROGRAM METADATA14#==============================================================================15METADATA = dict([16 ('package', 'EstatNet'),17 ('date', 'Mon Dec 18 17:28:29 2017'),18 ('url', 'https://github.com/gjacopo/EstatNet'),19 ('organization', 'European Union'),20 ('license', 'European Union Public Licence (EUPL)'),21 ('version', '0.1'),22 ('description', 'Tools for webscraping Eurostat website'),23 ('credits', ['gjacopo']),24 ('contact', 'jacopo.grazzini@ec.europa.eu'),25 ])26PACKAGES = [27 ]28#==============================================================================29# GENERIC WARNING/ERROR CLASSES30#==============================================================================31class ENetError(Exception):32 """Base class for exceptions in this module."""33 def __init__(self, msg, expr=None): 34 self.msg = msg35 if expr is not None: self.expr = expr36 def __str__(self): return repr(self.msg)37class ENetWarning(Warning):38 """Base class for warnings in this module."""39 def __init__(self, msg, expr=None): 40 self.msg = msg41 if expr is not None: self.expr = expr42 def __str__(self): return repr(self.msg)43#==============================================================================44# GENERIC XPATH BUILDER45#==============================================================================46 47class ENetXpath():48 """Class providing with the main (static) method (so-called 'xpath.create') 49 function used to "automatically" generate most of the rules that are needed50 to extract structured contents from webpage. 51 """52 @staticmethod53 def create(node=None, tag=None, first=None, last=None, identifier=None, 54 following_sibling=None, following=None, preceding_sibling=None, preceding=None, 55 ancestor=None, ancestor_self=None, descendant=None, descendant_self=None,56 parent=None, child=None, sep='/'):57 """Static method for generating generic xpath based on simple rules.58 59 >>> path = xpath.create(node, tag, first, last, identifier, 60 parent, preceding_sibling, preceding, ancestor,61 child, following_sibling, following, descendant, sep) 62 Arguments63 ---------64 node : str65 main (starting) node; default: None.66 tag : str 67 ending tag; default: None.68 first : str 69 tag/anchor "childing" the part to extract; default: None.70 last : str 71 tag/anchor parenting the part to extract; default: None.72 identifier : str 73 identifier (=condition) for the (FIRST,LAST) anchors; default: None.74 parent : str 75 parent axe: True (simple '/' or '//' relation) or expression of the 76 relation; default: None. 77 preceding_sibling : str 78 preceding-sibling axe: True or expression of the relation (used with 79 "preceding-sibling" keyword); default: None.80 preceding : str 81 preceding axe: True or expression of the relation (used with "preceding" 82 keyword); default: None.83 ancestor, ancestor_self : str 84 ancestor (or self) axes: True or expression of the relation (used with 85 "ancestor" and "ancestor-or-self" keywords respectively); default: None.86 child : str 87 child axe: True (simple '/' or '//' relation) or expression of the 88 relation; default: None. 89 following_sibling : str 90 following-sibling axe: True or expression of the relation (used with 91 "following-sibling" keyword); default: None.92 following : str 93 following axe: True or expression of the relation (used with "following" 94 keyword); default: None.95 descendant, descendant_self : str 96 descendant (or self) axes: True or expression of the relation (used with 97 "descendant" and "descendant-or-self" keywords respectively); default: None.98 sep:99 ; default: '/'.100 101 Returns102 -------103 path : str104 `xpath` formatted path to be used so as to extract a specific field 105 from a webpage.106 """107 ## default settings108 if last not in (None,'') and all([kw in (None,False,'') for kw in [preceding_sibling, preceding, ancestor, parent, ancestor_self]]):109 #warn(SXrapWarning("Parameter PRECEDING set to True with LAST"))110 preceding=True111 if first not in (None,'') and all([kw in (None,False,'') for kw in [following_sibling, following, child, descendant, descendant_self]]):112 #warn(SXrapWarning("Parameter CHILD set to True with FIRST"))113 child=True114 ## check115 if sum([kw not in ('', None) for kw in [preceding_sibling, preceding, ancestor, parent]]) > 1:116 raise ENetError("Incompatible keyword parameters (PRECEDING, PRECEDING_SIBLING, ANCESTOR, ANCESTOR_SELF, PARENT)") 117 elif sum([kw not in ('', None) for kw in [following_sibling, following, child, descendant]]) > 1:118 raise ENetError("Incompatible keyword parameters (FOLLOWING, FOLLOWING_SIBLING, DESCENDANT, DESCENDANT_SELF, CHILD)") 119 if sep not in (None,''): SEP=sep120 else: SEP='/'121 PARENTSEP = SEP122 if preceding_sibling in ('/', '//'): preceding_sibling, PARENTSEP = True, preceding_sibling123 elif preceding in ('/', '//'): preceding, PARENTSEP = True, preceding124 elif ancestor in ('/', '//'): ancestor, PARENTSEP = True, ancestor125 elif parent in ('/', '//'): parent, PARENTSEP = True, parent126 CHILDSEP = SEP127 if following_sibling in ('/', '//'): following_sibling, CHILDSEP = True, following_sibling128 elif following in ('/', '//'): following, CHILDSEP = True, following129 elif descendant in ('/', '//'): descendant, CHILDSEP = True, descendant130 elif child in ('/', '//'): child, CHILDSEP = True, child131 if not (last in (None,'') or all([kw in (None,True,'') for kw in [preceding_sibling, preceding, parent, ancestor, ancestor_self]])):132 raise ENetError("Instructions for (PRECEDING, PRECEDING_SIBLING, PARENT, ANCESTOR, ANCESTOR_SELF) incompatible with keyword parameter LAST")133 elif last in (None,'') and all([kw in (None,True,'') for kw in [preceding_sibling, preceding, parent, ancestor, ancestor_self]]):134 #SXrapWarning("Instructions for (PRECEDING, PRECEDING_SIBLING, ANCESTOR, PARENT) ignored in absence of parameter LAST")135 preceding_sibling = preceding = parent = ancestor = None136 if not (first in (None,'') or all([kw in (None,True,'') for kw in [following_sibling, following, child, descendant, descendant_self]])):137 raise ENetError("Instructions for (FOLLOWING, FOLLOWING_SIBLING, CHILD, DESCENDANT, DESCENDANT_SELF) incompatible with keyword parameter FIRST")138 elif first in (None,'') and all([kw in (None,True,'') for kw in [following_sibling, following, child, descendant, descendant_self]]):139 # this may ignored in case the default setting on first above is actually run140 #SXrapWarning("Parameters (FOLLOWING, FOLLOWING_SIBLING, DESCENDANT, CHILD) ignored in absence of parameter FIRST")141 following_sibling = following = child = descendant = None142 ## set143 prec, follow = '', ''144 if preceding_sibling not in ('', None): prec, parent = 'preceding-sibling::', preceding_sibling145 elif preceding not in ('', None): prec, parent = 'preceding::', preceding146 elif ancestor not in ('', None): prec, parent = 'ancestor::', ancestor147 elif ancestor_self not in ('', None): prec, parent = 'ancestor-or-self::', ancestor_self148 elif parent not in ('', None): prec = 'parent::' # parent unchanged149 else: parent = None 150 if following_sibling not in ('', None): follow, child = 'following-sibling::', following_sibling151 elif following not in ('', None): follow, child = 'following::', following152 elif descendant not in ('', None): follow, child = 'descendant::', descendant153 elif descendant_self not in ('', None): follow, child = 'descendant-or-self::', descendant_self154 elif child not in ('', None): follow = '' # child unchanged155 else: child = None156 ## further check157 if not node in (None,'') and not node.startswith('/'): 158 node = '//%s' % node159 #if not tag in ('',None) and not tag.startswith('/'): 160 # tag = '//%s' %tag161 if not identifier in (None,'') and not identifier.startswith('['): 162 identifier = '[%s]' % identifier163 ## run164 # initialise xrule165 xrule=''166 if not last in (None,''):167 if not identifier in (None,''):168 last = '%s%s' % (last,identifier)169 if not (first is None or (parent in (None,'') or isinstance(parent,bool))):170 if follow=='' and not last.startswith('/'): last = '*[%s%s]' % (PARENTSEP,last)171 else: last = '*[%s]' % last 172 elif not first is None:173 if follow=='' and not last.startswith('/'): last = '%s%s' % (PARENTSEP,last)174 # else: do nothing! 175 elif not node in (None,''):176 last = '%s%s' % (SEP, last) # not PARENTSEP!177 else:178 last = '//%s' % last # '%s%s' % (SEP, last)179 if prec!='' and parent not in ('', None):180 last = '%s%s%s' % (last, PARENTSEP, prec)181 elif not child in (None,True,''): 182 last = '%s%s' % (last, PARENTSEP)183 elif not (parent in (None, '') or isinstance(parent,bool)): #not all([kw in (True, '', None) for kw in [preceding_sibling, preceding, parent, ancestor]]):184 last = '%s%s' % (prec, parent) 185 if not identifier in (None,''):186 last = '%s%s' % (last,identifier)187 if not first is None :188 last = '*[%s]' % last 189 if not first in (None,''):190 if not identifier in (None,''):191 first = '%s%s' % (first,identifier)192 if not (last is None or (child in (None, '') or isinstance(child,bool))):193 if prec=='' and not first.startswith('/'): first = '*[%s%s]' % (CHILDSEP,first)194 else: first = '*[%s]' % first 195 elif not last is None:196 if prec=='' and not first.startswith('/'): first = '%s%s' % (CHILDSEP,first)197 elif node in (None,''): first = '//%s' % first198 elif not node in (None,''):199 first = '%s%s' % (SEP, first)200 else:201 first = '//%s' % first202 if follow!='' and child not in ('', None):203 first = '%s%s%s' % (first, CHILDSEP, follow)204 elif not (parent in (None,'') or isinstance(parent,bool)): 205 first = '%s%s' % (first, CHILDSEP)206 elif not (child in (None, '') or isinstance(child,bool)): #not all([kw in (True, '', None) for kw in [following_sibling, following, child, descendant]]):207 first = '%s%s' % (follow, child)208 if not identifier in (None,''):209 first = '%s%s' % (first,identifier)210 if not (first in (None,'') or parent in (None,'') or isinstance(parent,bool)):211 if last in (None,'') and follow!='':212 xrule = '%s%s' % (xrule, first)213 elif last in (None,''):214 xrule = '%s%s*' % (xrule, first)215 elif last.startswith('/'):216 xrule = '%s%s*[%s]' % (xrule, first, last)217 else:218 xrule = '%s%s%s' % (xrule, first, last)219 elif not (last in (None,'') or child in (None,'') or isinstance(child,bool)):220 if first in (None,'') and prec!='':221 xrule = '%s%s' % (xrule, last)222 elif first in (None,''):223 xrule = '%s%s*' % (xrule, last)224 elif first.startswith('/'):225 xrule = '%s%s*[%s]' % (xrule, last, first)226 else:227 xrule = '%s%s%s' % (xrule, last, first)228 elif not all([kw in (None,False,'') for kw in [preceding_sibling, preceding, ancestor]]):229 xrule = '%s%s%s' % (xrule, last or '', first or ('*' if tag in ('',None) else '')) 230 elif not all([kw in (None,False,'') for kw in [following_sibling, following, descendant]]):231 xrule = '%s%s%s' % (xrule, first or '', last or ('*' if tag in ('',None) else '')) 232 else:233 xrule = '%s%s%s' % (xrule, last or '', first or '') 234 if not node in (None,''):235 if not (node.endswith('/') or node.endswith('/') or xrule.startswith('/')) or xrule.startswith('*'): 236 xrule = '%s%s%s' % (node, SEP, xrule)237 elif node.endswith('::') and xrule.startswith('/') and not xrule.startswith('*'):238 xrule = '%s*%s' % (node, xrule)239 elif not (node.endswith('::') or node.endswith('/') or xrule.startswith('/') or xrule.startswith('*')):240 xrule = '%s%s*%s' % (node, SEP, xrule)241 else: 242 xrule = '%s%s' % (node, xrule) 243 if not tag in ('',None):244 if not (xrule.endswith('::') or xrule.endswith('/') or tag.startswith('/') or tag.startswith('*')):245 xrule = '%s%s%s' % (xrule, SEP, tag)246 elif xrule.endswith('::') and tag.startswith('/') and not tag.startswith('*'):247 xrule = '%s*%s' % (xrule, tag)248 elif not (xrule.endswith('::') or xrule.endswith('/') or tag.startswith('/') or tag.startswith('*')):249 xrule = '%s%s*%s' % (xrule, SEP, tag)250 else: 251 xrule = '%s%s' % (xrule, tag) ...

Full Screen

Full Screen

adjacent_tests.py

Source:adjacent_tests.py Github

copy

Full Screen

...43 assert len(siblings) == 244 assert all(isinstance(sibling, Div) for sibling in siblings)45class TestAdjacentFollowingSibling(object):46 def test_gets_immediate_following_sibling_of_an_element_by_default(self, browser):47 assert browser.div(id='first_sibling').following_sibling().id == 'between_siblings1'48 assert isinstance(browser.div(id='first_sibling').following_sibling(), HTMLElement)49 def test_accepts_index_argument(self, browser):50 assert browser.div(id='first_sibling').following_sibling(index=2).id == 'between_siblings2'51 assert isinstance(browser.div(id='first_sibling').following_sibling(index=2), HTMLElement)52 def test_accepts_tag_name_argument(self, browser):53 assert browser.div(id='first_sibling').following_sibling(54 tag_name='div').id == 'second_sibling'55 assert isinstance(browser.div(id='first_sibling').following_sibling(tag_name='div'), Div)56 def test_accepts_class_name_argument_for_single_class(self, browser):57 exp = 'second_sibling'58 assert browser.div(id='first_sibling').following_sibling(class_name='b').id == exp59 def test_accepts_index_and_tag_name_arguments(self, browser):60 assert browser.div(id='first_sibling').following_sibling(tag_name='div',61 index=1).id == 'third_sibling'62 assert isinstance(63 browser.div(id='first_sibling').following_sibling(tag_name='div', index=1), Div)64 def test_accepts_text_as_regexp(self, browser):65 exp = 'third_sibling'66 assert browser.div(id='first_sibling').following_sibling(text=re.compile('T')).id == exp67 def test_accepts_text_as_string(self, browser):68 assert browser.div(id='first_sibling').following_sibling(text='Third').id == 'third_sibling'69 def test_does_not_error_when_no_next_sibling_of_an_index_exists(self, browser):70 assert not browser.body().following_sibling(index=1).exists71 def test_does_not_error_when_no_next_sibling_of_a_tag_name_exists(self, browser):72 assert not browser.div(id='first_sibling').following_sibling(tag_name='table').exists73class TestAdjacentFollowingSiblings(object):74 def test_gets_collection_of_subsequent_siblings_of_an_element_by_default(self, browser):75 assert isinstance(browser.div(id='second_sibling').following_siblings(),76 HTMLElementCollection)77 assert len(browser.div(id='second_sibling').following_siblings()) == 278 def test_accepts_tag_name_argument(self, browser):79 assert len(browser.div(id='second_sibling').following_siblings(tag_name='div')) == 180 assert isinstance(browser.div(id='second_sibling').following_siblings(tag_name='div')[0],81 Div)82 def test_accepts_class_name_argument(self, browser):83 # assert len(browser.div(id='second_sibling').following_siblings(class_name='b')) == 184 assert isinstance(browser.div(id='second_sibling').following_siblings(class_name='b')[0],85 Div)86 def test_accepts_class_name_argument_for_multiple_classes(self, browser):...

Full Screen

Full Screen

automation_practice_form.py

Source:automation_practice_form.py Github

copy

Full Screen

1from selene import have2from selene.support.conditions.have import text3from selene.support.shared import browser4browser.open('https://demoqa.com/automation-practice-form')5browser.element('h5').should(have.text('Student Registration Form'))6browser.element('#firstName').type('Ivan')7browser.element('#lastName').type('Ivanov')8browser.element('#userEmail').type('ivanov@mail.ru')9browser.element('[value=Male]').parent_element.click()10browser.element('#userNumber').type('9151001010')11browser.element('#dateOfBirthInput').click()12browser.element('option[value="1999"]').click()13browser.elements('.react-datepicker__month-select option').element_by(text('December')).click()14browser.element('.react-datepicker__day--031:not(.react-datepicker__day--outside-month)').click()15browser.element('#subjectsInput').type('Maths').press_enter()16browser.elements('[for^=hobbies-checkbox]').element_by(text('Sports')).click()17# TODO browser.elements('#uploadPicture')18browser.element('#currentAddress').type('Current Address')19browser.element('#state input').scroll_to().type('NCR').press_enter()20browser.element('#city input').type('Delhi').press_enter().press_enter()21browser.element('.modal-content').should(have.text('Thanks for submitting the form'))22elements = browser.elements('td')23elements.element_by(text('Student Name')).following_sibling.should(have.text('Ivan Ivanov'))24elements.element_by(text('Student Email')).following_sibling.should(have.text('ivanov@mail.ru'))25elements.element_by(text('Gender')).following_sibling.should(have.text('Male'))26elements.element_by(text('Mobile')).following_sibling.should(have.text('9151001010'))27elements.element_by(text('Date of Birth')).following_sibling.should(have.text('31 December,1999'))28elements.element_by(text('Subjects')).following_sibling.should(have.text('Maths'))29elements.element_by(text('Hobbies')).following_sibling.should(have.text('Sports'))30# TODO elements.element_by(text('Picture')).following_sibling.should(have.text('pic.png'))31elements.element_by(text('Address')).following_sibling.should(have.text('Current Address'))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful