import time import random def type(e, text, delay=0.1): for c in text: e.type(c) sleep(0, delay) def filter_by_value(elements, values): if not isinstance(values, list): values = [values] return [e for e in elements if e.value.strip() in values] def sleep(min_delay, random_delay): time.sleep(min_delay + random_delay * random.random()) def _get_tag_ids_single(html, tag): start_idx = 0 res = [] while html.find(tag, start_idx) >= 0: idx = html.find(tag, start_idx) s = idx while html[s] != '<': s -= 1 e = idx while html[e] != '>': e += 1 id_start_idx = html.find('id="', s) + 4 id_end_idx = html.find('"', id_start_idx) if id_start_idx < s or id_start_idx > e or id_end_idx < s or id_end_idx > e: print 'could not find id in %s' % html[s:e] start_idx = idx + 1 continue res.append(html[id_start_idx:id_end_idx]) start_idx = idx + 1 return res def get_tag_ids(html, tags): if isinstance(tags, list): for tag in tags: res = _get_tag_ids_single(html, tag) if res: return res return [] else: return _get_tag_ids_single(html, tag)