util.py 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import time
  2. import random
  3. def type(e, text, delay=0.1):
  4. for c in text:
  5. e.type(c)
  6. sleep(0, delay)
  7. def filter_by_value(elements, value):
  8. return [e for e in elements if e.value.strip() == value]
  9. def sleep(min_delay, random_delay):
  10. time.sleep(min_delay + random_delay * random.random())
  11. def get_tag_ids(html, tag):
  12. start_idx = 0
  13. res = []
  14. while html.find(tag, start_idx) >= 0:
  15. idx = html.find(tag, start_idx)
  16. s = idx
  17. while html[s] != '<':
  18. s -= 1
  19. e = idx
  20. while html[e] != '>':
  21. e += 1
  22. id_start_idx = html.find('id="', s) + 4
  23. id_end_idx = html.find('"', id_start_idx)
  24. if id_start_idx < s or id_start_idx > e or id_end_idx < s or id_end_idx > e:
  25. print 'could not find id in %s' % html[s:e]
  26. start_idx = idx + 1
  27. continue
  28. res.append(html[id_start_idx:id_end_idx])
  29. start_idx = idx + 1
  30. return res