69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
import random
|
|
import time
|
|
from selenium import webdriver
|
|
|
|
headless = False
|
|
chromeProfilePath = r"/home/.config/google-chrome/linkedin_profile"
|
|
|
|
def is_scrollable(element):
|
|
"""Controlla se un elemento è scrollabile."""
|
|
scroll_height = element.get_attribute("scrollHeight")
|
|
client_height = element.get_attribute("clientHeight")
|
|
return int(scroll_height) > int(client_height)
|
|
|
|
def scroll_slow(driver, scrollable_element, start=0, end=3600, step=100, reverse=False):
|
|
if reverse:
|
|
start, end = end, start
|
|
step = -step
|
|
|
|
if step == 0:
|
|
raise ValueError("Step cannot be zero.")
|
|
|
|
# Script di scrolling che utilizza scrollTop
|
|
script_scroll_to = "arguments[0].scrollTop = arguments[1];"
|
|
|
|
try:
|
|
if scrollable_element.is_displayed():
|
|
if not is_scrollable(scrollable_element):
|
|
print("The element is not scrollable.")
|
|
return
|
|
if (step > 0 and start >= end) or (step < 0 and start <= end):
|
|
print("No scrolling will occur due to incorrect start/end values.")
|
|
return
|
|
for position in range(start, end, step):
|
|
try:
|
|
driver.execute_script(script_scroll_to, scrollable_element, position)
|
|
except Exception as e:
|
|
print(f"Error during scrolling: {e}")
|
|
time.sleep(random.uniform(1.0, 2.6))
|
|
driver.execute_script(script_scroll_to, scrollable_element, end)
|
|
time.sleep(1)
|
|
else:
|
|
print("The element is not visible.")
|
|
except Exception as e:
|
|
print(f"Exception occurred: {e}")
|
|
|
|
def chromeBrowserOptions():
|
|
options = webdriver.ChromeOptions()
|
|
options.add_argument('--no-sandbox')
|
|
options.add_argument("--ignore-certificate-errors")
|
|
options.add_argument("--disable-extensions")
|
|
options.add_argument('--disable-gpu')
|
|
options.add_argument('--disable-dev-shm-usage')
|
|
options.add_argument('--remote-debugging-port=9222')
|
|
if(headless):
|
|
options.add_argument("--headless")
|
|
options.add_argument("--start-maximized")
|
|
options.add_argument("--disable-blink-features")
|
|
options.add_argument("--disable-blink-features=AutomationControlled")
|
|
options.add_experimental_option('useAutomationExtension', False)
|
|
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
|
if(len(chromeProfilePath)>0):
|
|
initialPath = chromeProfilePath[0:chromeProfilePath.rfind("/")]
|
|
profileDir = chromeProfilePath[chromeProfilePath.rfind("/")+1:]
|
|
options.add_argument('--user-data-dir=' +initialPath)
|
|
options.add_argument("--profile-directory=" +profileDir)
|
|
else:
|
|
options.add_argument("--incognito")
|
|
return options
|