Skip to content

Instantly share code, notes, and snippets.

@dancipher
Created June 28, 2012 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dancipher/1540f8b3bfc866b907f8 to your computer and use it in GitHub Desktop.
Save dancipher/1540f8b3bfc866b907f8 to your computer and use it in GitHub Desktop.
JS Events
491
492 def fire_all_events(self):
493 self.events_cache[self.url] = True
494 listeners = list(self.webframe.findAllElements('a,button,img,select,input'))
495 for index, listener in enumerate(listeners[:5]):
496 c_dict = {}
497 for k in list(listener.attributeNames()):
498 c_dict[str(k).lower()] = str(listener.attribute(k))
499 listener.removeAttribute(k)
500 for cc, yy in c_dict.items():
501 listener.setAttribute(cc, yy)
502 last = ""
503 max_ = 5
504 current_ = 0
505 for l in listeners:
506 if current_ >= max_:
507 break
508 else:
509 current_ += 1
510 data = str(l.toOuterXml())
511 if last:
512 ratio = Levenshtein.ratio(data, last)
513 if ratio >= 0.7:
514 last = data
515 continue
516 else:
517 last = data
518 else:
519 last = data
520 event = ""
521 attrs = list(l.attributeNames())
522 for a in attrs:
523 a = str(a)
524 if a.startswith("on"):
525 event = a[2:]
526 else:
527 continue
528 if not event:
529 continue
530 else:
531 self.fire_event(l, event)
884 def fire_event(self, element, event='click'):
885 """
886 Fire a Javascript Event. This does not include native events
887
888 @param element: QWebElement
889 @param event: click (optional)
890
891 @return bool
892 """
893 before_html, before_url = self.html, self.url
894 try:
895 self.click_element(element, event=event, timeout=2, wait_requests=0, wait_load=True)
896 self.webframe.setHtml(before_html, baseUrl=str(before_url))
897 except BrowserTimeout, e:
898 pass
921 def click_element_link(self, element, timeout=None):
922 """
923 Click a link and wait for the page to load.
924
925 @param selector: WebKit xpath selector to an element
926 @param: timeout timeout to wait in seconds
927 """
928 return self.click_element(element, wait_load=True, timeout=timeout)
900 def click_element(self, element, wait_load=False, event='click', wait_requests=None, timeout=None):
901 """
902 Click on an element by using raw javascript WebKit.click() method.
903
904 @param element: QWebElement object
905 @param wait_load: If True, it will wait until a new page is loaded.
906 @param timeout: Seconds to wait for the page to load before
907 raising an exception.
908 @param wait_requests: How many requests to wait before returning. Useful
909 for AJAX requests.
910
911 """
912 jscode = (
913 "var e = document.createEvent('Events');e.initEvent( '%s', true, true );this.dispatchEvent(e);" %event
914 )
915 element.evaluateJavaScript(jscode)
916 time.sleep(0.5)
917 self.wait_requests(wait_requests)
918 if wait_load:
919 return self._wait_load(timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment