Skip to content

Commit b38d12d

Browse files
authored
simplification of xpath (#227)
* updates to the xpath * linting changes * updated the test file * saving uv changes * removing try except pass
1 parent 4b3264d commit b38d12d

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

tests/core/test_tab.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ async def test_xpath(browser: zd.Browser) -> None:
7979
assert result.tag == "li"
8080
assert result.text == "Apples"
8181

82+
async def test_xpath_no_results(browser: zd.Browser) -> None:
83+
tab = await browser.get(sample_file("groceries.html"))
84+
85+
results = await tab.xpath('//li[@aria-label="Nonexistent Item"]')
86+
87+
assert len(results) == 0
88+
8289

8390
async def test_add_handler_type_event(browser: zd.Browser) -> None:
8491
tab = await browser.get(sample_file("groceries.html"))

uv.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zendriver/core/tab.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,18 @@ async def xpath(self, xpath: str, timeout: float = 2.5) -> List[Element]: # noq
382382
:rtype:
383383
"""
384384
items: List[Element] = []
385-
try:
386-
await self.send(cdp.dom.enable(), True)
387-
loop = asyncio.get_running_loop()
388-
while not items:
389-
start_time = loop.time()
390-
try:
391-
items = await self.find_all(xpath, timeout=0)
392-
except TimeoutError:
393-
pass
385+
loop = asyncio.get_running_loop()
386+
start_time = loop.time()
394387

395-
await self.sleep(0.1)
396-
if loop.time() - start_time > timeout:
397-
break
398-
finally:
388+
while (loop.time() - start_time) < timeout and len(items) == 0:
389+
try:
390+
await self.send(cdp.dom.enable(), True)
391+
items = await self.find_all(xpath, timeout=0)
392+
except Exception:
393+
items = [] # find_elements_by_text may raise exception
394+
399395
await self.disable_dom_agent()
396+
400397
return items
401398

402399
async def get(

0 commit comments

Comments
 (0)