from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time

# LinkedIn company page URL
URL = "https://www.linkedin.com/company/euipo/posts/?feedView=all"

# Setup Chrome WebDriver
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

# Function to log in to LinkedIn (manual login for CAPTCHA handling)
def linkedin_login():
    driver.get("https://www.linkedin.com/login")
    print("Please log in to LinkedIn manually. You have 60 seconds.")
    try:
        # Wait until a common element on the LinkedIn feed/homepage is visible
        WebDriverWait(driver, 60).until(
            EC.presence_of_element_located((By.ID, "global-nav-typeahead"))
        )
        print("Login successful (main navigation element found).")
    except Exception:
        print("Login failed or timed out after 60 seconds. Please check the browser manually.")
        raise Exception("Login timeout: Could not find expected element after login.")

# Function to scroll and load all posts
def scroll_and_load_posts():
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(5)  # Increased wait time for content to load
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height

# Function to extract post link using data-urn
def extract_post_link(post):
    try:
        # Extract the data-urn attribute from the main post container
        post_urn = post.get_attribute('data-urn')
        if not post_urn or not post_urn.startswith('urn:li:activity:'):
            print("Could not find valid data-urn for post.")
            return None

        # Construct the direct post link
        post_link = f"https://www.linkedin.com/feed/update/{post_urn}/"
        print(f"Extracted post link: {post_link}")
        return post_link
    except Exception as e:
        print(f"Error extracting post link: {e}")
        return None

# Function to extract post data (simplified to only extract post link)
def extract_post_data(post):
    try:
        # Extract post link using data-urn
        print("Attempting to extract post link...")
        post_link = extract_post_link(post)
        if not post_link:
            return None

        # Return post data as a dictionary with only the post link
        return {
            "Post Link": post_link
        }
    except Exception as e:
        print(f"Error extracting post data: {e}")
        return None

# Main scraping function
def scrape_linkedin_posts():
    # Log in to LinkedIn
    linkedin_login()

    # Navigate to the company posts page
    driver.get(URL)
    time.sleep(5)  # Wait for the page to load

    # Scroll and load all posts
    scroll_and_load_posts()

    # Wait for at least one post to be present
    print("Waiting for posts to load...")
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'feed-shared-update-v2')]"))
    )

    # Find all post elements
    print("Attempting to find post elements...")
    posts = driver.find_elements(By.XPATH, "//div[contains(@class, 'feed-shared-update-v2') and @data-urn]")
    print(f"Found {len(posts)} posts.")

    # Extract data from each post
    post_data = []
    processed_urns = set()  # To avoid duplicates
    for i, post in enumerate(posts):
        print(f"\nProcessing post {i + 1}...")
        post_urn = post.get_attribute('data-urn')
        if post_urn and post_urn not in processed_urns:
            data = extract_post_data(post)
            if data:
                post_data.append(data)
                processed_urns.add(post_urn)
        else:
            print(f"Post {i + 1} skipped due to duplicate or missing data-urn.")

    # Create a DataFrame and save to Excel
    if post_data:
        df = pd.DataFrame(post_data)
        df.to_excel("linkedin_euipo_posts.xlsx", index=False)
        print("Data saved to linkedin_euipo_posts.xlsx")
    else:
        print("No posts found.")

# Run the script
try:
    scrape_linkedin_posts()
finally:
    driver.quit()



can you rewrite this code so that the links from see more are fetched 

and put into excel