Add only_visible to find_all_elements

This commit is contained in:
Florian Bruhin 2016-07-28 10:34:51 +02:00
parent 5b943a431e
commit 540c62c232
5 changed files with 18 additions and 10 deletions

View File

@ -633,8 +633,13 @@ class AbstractTab(QWidget):
def set_html(self, html, base_url):
raise NotImplementedError
def find_all_elements(self, selector):
"""Find all HTML elements matching a given selector."""
def find_all_elements(self, selector, *, only_visible=False):
"""Find all HTML elements matching a given selector.
Args:
selector: The CSS selector to search for.
only_visible: Only show elements which are visible on screen.
"""
raise NotImplementedError
def __repr__(self):

View File

@ -656,8 +656,8 @@ class HintManager(QObject):
def _init_elements(self):
"""Initialize the elements and labels based on the context set."""
selector = webelem.SELECTORS[self._context.group]
elems = self._context.tab.find_all_elements(selector)
elems = [e for e in elems if e.is_visible()]
elems = self._context.tab.find_all_elements(selector,
only_visible=True)
filterfunc = webelem.FILTERS.get(self._context.group, lambda e: True)
elems = [e for e in elems if filterfunc(e)]
if not elems:

View File

@ -426,10 +426,9 @@ class WebElementWrapper(collections.abc.MutableMapping):
rect.setHeight(rect.height() / zoom)
return rect
def is_visible(self):
"""Check if the given element is visible in the frame."""
def is_visible(self, mainframe):
"""Check if the given element is visible in the given frame."""
self._check_vanished()
mainframe = self._elem.webFrame()
# CSS attributes which hide an element
hidden_attributes = {
'visibility': 'hidden',

View File

@ -557,7 +557,7 @@ class WebKitTab(browsertab.AbstractTab):
def set_html(self, html, base_url):
self._widget.setHtml(html, base_url)
def find_all_elements(self, selector):
def find_all_elements(self, selector, *, only_visible=False):
mainframe = self._widget.page().mainFrame()
if mainframe is None:
raise WebTabError("No frame focused!")
@ -567,6 +567,10 @@ class WebKitTab(browsertab.AbstractTab):
for f in frames:
for elem in f.findAllElements(selector):
elems.append(webelem.WebElementWrapper(elem))
if only_visible:
elems = [e for e in elems if e.is_visible(mainframe)]
return elems
@pyqtSlot()

View File

@ -672,9 +672,9 @@ class TestRectOnView:
def test_passed_geometry(self, stubs, js_rect):
"""Make sure geometry isn't called when a geometry is passed."""
frame = stubs.FakeWebFrame(QRect(0, 0, 200, 200))
raw_elem = get_webelem(frame=frame, js_rect_return=js_rect)._elem
elem = get_webelem(frame=frame, js_rect_return=js_rect)
rect = QRect(10, 20, 30, 40)
assert webelem.rect_on_view(raw_elem, elem_geometry=rect) == rect
assert elem.rect_on_view(elem_geometry=rect) == rect
assert not raw_elem.geometry.called
@pytest.mark.parametrize('js_rect', [None, {}])