stop scrolling when cant scroll (#771)

This commit is contained in:
LawyZheng
2024-09-04 02:31:04 +08:00
committed by GitHub
parent 2ba0309602
commit 069597e52e
2 changed files with 63 additions and 21 deletions

View File

@@ -1372,6 +1372,11 @@ function drawBoundingBoxes(elements) {
addHintMarkersToPage(hintMarkers); addHintMarkersToPage(hintMarkers);
} }
function buildElementsAndDrawBoundingBoxes() {
var elementsAndResultArray = buildTreeFromBody();
drawBoundingBoxes(elementsAndResultArray[0]);
}
function captchaSolvedCallback() { function captchaSolvedCallback() {
console.log("captcha solved"); console.log("captcha solved");
if (!window["captchaSolvedCounter"]) { if (!window["captchaSolvedCounter"]) {
@@ -1556,8 +1561,7 @@ function scrollToTop(draw_boxes) {
removeBoundingBoxes(); removeBoundingBoxes();
window.scroll({ left: 0, top: 0, behavior: "instant" }); window.scroll({ left: 0, top: 0, behavior: "instant" });
if (draw_boxes) { if (draw_boxes) {
var elementsAndResultArray = buildTreeFromBody(); buildElementsAndDrawBoundingBoxes();
drawBoundingBoxes(elementsAndResultArray[0]);
} }
return window.scrollY; return window.scrollY;
} }
@@ -1572,12 +1576,30 @@ function scrollToNextPage(draw_boxes) {
behavior: "instant", behavior: "instant",
}); });
if (draw_boxes) { if (draw_boxes) {
var elementsAndResultArray = buildTreeFromBody(); buildElementsAndDrawBoundingBoxes();
drawBoundingBoxes(elementsAndResultArray[0]);
} }
return window.scrollY; return window.scrollY;
} }
function isWindowScrollable() {
// Check if the body's overflow style is set to hidden
const bodyOverflow = window.getComputedStyle(document.body).overflow;
const htmlOverflow = window.getComputedStyle(
document.documentElement,
).overflow;
// Check if the document height is greater than the window height
const isScrollable =
document.documentElement.scrollHeight > window.innerHeight;
// If the overflow is set to 'hidden' or there is no content to scroll, return false
if (bodyOverflow === "hidden" || htmlOverflow === "hidden" || !isScrollable) {
return false;
}
return true;
}
function scrollToElementBottom(element, page_by_page = false) { function scrollToElementBottom(element, page_by_page = false) {
const top = page_by_page const top = page_by_page
? element.clientHeight + element.scrollTop ? element.clientHeight + element.scrollTop

View File

@@ -92,25 +92,37 @@ class SkyvernFrame:
assert isinstance(skyvern_page.frame, Page) assert isinstance(skyvern_page.frame, Page)
screenshots: List[bytes] = [] screenshots: List[bytes] = []
scroll_y_px_old = -30.0 if await skyvern_page.is_window_scrollable():
scroll_y_px = await skyvern_page.scroll_to_top(draw_boxes=draw_boxes) scroll_y_px_old = -30.0
# Checking max number of screenshots to prevent infinite loop scroll_y_px = await skyvern_page.scroll_to_top(draw_boxes=draw_boxes)
# We are checking the difference between the old and new scroll_y_px to determine if we have reached the end of the # Checking max number of screenshots to prevent infinite loop
# page. If the difference is less than 25, we assume we have reached the end of the page. # We are checking the difference between the old and new scroll_y_px to determine if we have reached the end of the
while abs(scroll_y_px_old - scroll_y_px) > 25 and len(screenshots) < max_number: # page. If the difference is less than 25, we assume we have reached the end of the page.
while abs(scroll_y_px_old - scroll_y_px) > 25 and len(screenshots) < max_number:
screenshot = await SkyvernFrame.take_screenshot(page=skyvern_page.frame, full_page=False)
screenshots.append(screenshot)
scroll_y_px_old = scroll_y_px
LOG.debug("Scrolling to next page", url=url, num_screenshots=len(screenshots))
scroll_y_px = await skyvern_page.scroll_to_next_page(draw_boxes=draw_boxes)
LOG.debug(
"Scrolled to next page",
scroll_y_px=scroll_y_px,
scroll_y_px_old=scroll_y_px_old,
)
if draw_boxes:
await skyvern_page.remove_bounding_boxes()
await skyvern_page.scroll_to_top(draw_boxes=False)
else:
if draw_boxes:
await skyvern_page.build_elements_and_draw_bounding_boxes()
LOG.debug("Page is not scrollable", url=url, num_screenshots=len(screenshots))
screenshot = await SkyvernFrame.take_screenshot(page=skyvern_page.frame, full_page=False) screenshot = await SkyvernFrame.take_screenshot(page=skyvern_page.frame, full_page=False)
screenshots.append(screenshot) screenshots.append(screenshot)
scroll_y_px_old = scroll_y_px
LOG.debug("Scrolling to next page", url=url, num_screenshots=len(screenshots)) if draw_boxes:
scroll_y_px = await skyvern_page.scroll_to_next_page(draw_boxes=draw_boxes) await skyvern_page.remove_bounding_boxes()
LOG.debug(
"Scrolled to next page",
scroll_y_px=scroll_y_px,
scroll_y_px_old=scroll_y_px_old,
)
if draw_boxes:
await skyvern_page.remove_bounding_boxes()
await skyvern_page.scroll_to_top(draw_boxes=False)
return screenshots return screenshots
@staticmethod @staticmethod
@@ -205,3 +217,11 @@ class SkyvernFrame:
""" """
js_script = "() => removeBoundingBoxes()" js_script = "() => removeBoundingBoxes()"
await self.frame.evaluate(js_script) await self.frame.evaluate(js_script)
async def build_elements_and_draw_bounding_boxes(self) -> None:
js_script = "() => buildElementsAndDrawBoundingBoxes()"
await self.frame.evaluate(js_script)
async def is_window_scrollable(self) -> bool:
js_script = "() => isWindowScrollable()"
return await self.frame.evaluate(js_script)