import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

# Create ChromeOptions object to configure headless mode
chrome_options = Options()
# chrome_options.add_argument('--headless')

# Path to the Chrome WebDriver executable
chromedriver_path = 'C:\MITWPU\Coding\Python\Test\chromedriver-win64\chromedriver.exe'

# Initialize a Selenium WebDriver (you need to have a compatible driver installed)
# Download the driver from https://sites.google.com/chromium.org/driver/
driver = webdriver.Chrome(options=chrome_options)

# Navigate to the website
driver.get("https://iunlocker.com/check_icloud.php")
def fetchData(num):
    try:
        # Wait for the input field to become available
        imei_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.NAME, "imeip"))
        )

        # Input the IMEI number (replace 'your_imei' with the actual IMEI)
        imei_input.send_keys(str(num))

        # Submit the form (including handling reCAPTCHA)
        submit_button = driver.find_element(By.CLASS_NAME, "button-go")
        submit_button.click()

        # Wait for the results popup to appear
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CLASS_NAME, "dscrInformation__item-value"))
        )

        # Wait for the results to load (adjust the sleep time as needed)
        time.sleep(3)

        # Get the content of the results popup
        content = driver.find_element(By.CLASS_NAME, "dscrInformation__items").get_attribute("innerHTML")

        # Parse the HTML content of the popup
        soup = BeautifulSoup(content, 'html.parser')

        # Extract the text values
        items = soup.find_all('div', class_='dscrInformation__item-value')
        extracted_values = [item.get_text(strip=True) for item in items]

        return extracted_values

    except Exception as e:
        print("Error:", e)

    finally:
        # Close the popup and the main browser window
        driver.quit()



