def scrollnclick(driver): """ Scroll the page down and click 'Show more jobs' button if…
- User
def scroll_n_click(driver): """ Scroll the page down and click 'Show more jobs' button if available. Return True if more jobs are loaded, False if no more jobs are available. """ try: # Scroll to the bottom of the page driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(10) # Wait for the page to load new content # Check if the notification class is present try: if driver.find_elements(By.CSS_SELECTOR, ".inline-notification__text.text-sm.leading-regular"): return False # Stop if notification is found except NoSuchElementException: pass try: show_more_button = driver.find_element(By.XPATH, './/button[@aria-label="See more jobs"]') driver.execute_script("arguments[0].click();", show_more_button) # show_more_button.click() time.sleep(5) # Wait for new jobs to load after clicking return True # New jobs loaded except NoSuchElementException: pass # No "show more jobs" button found, just scroll to load more content
# Check if page height increased after scrolling previous_height = driver.execute_script("return document.body.scrollHeight") time.sleep(3) new_height = driver.execute_script("return document.body.scrollHeight") if new_height == previous_height: return False # No new content loaded return True # New content loaded except Exception as e: print(f"Exception during scrolling or clicking: {e}") return False # If an error occurs, assume no more jobs are availablecan you make this code more robust so that it only stop scrolling and clicking when the notificcation class apears on the page
- Assistant