commit 73a68c74c0f97f77c9c7a816dc770f45c1c176dc Author: Arne Weiss Date: Tue Apr 11 21:59:54 2023 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..283f43c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +geckodriver.log diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..43f9eeb --- /dev/null +++ b/Readme.md @@ -0,0 +1,76 @@ +# Selenium WebDriver Script for Visametric + +This project provides a Python script to automate the process of navigating and filling out a form on the Visametric website. It uses the Selenium WebDriver library and the geckodriver to control a Firefox browser. This README provides detailed instructions for setting up and running the script on Linux and macOS using Nix. + +## Table of Contents + +1. [Requirements](#requirements) +2. [Installation](#installation) + - [Linux](#linux) + - [macOS](#macos) +3. [Running the script](#running-the-script) +4. [Extending the script](#extending-the-script) +5. [Troubleshooting](#troubleshooting) + +## Requirements + +- Python 3.6 or later +- Firefox browser +- Nix package manager +- geckodriver (Firefox WebDriver for Selenium) + +## Installation + +### Linux + +1. Install the Nix package manager if you haven't already by running: + + curl -L https://nixos.org/nix/install | sh + +2. Add the following lines to your ~/.bashrc or ~/.zshrc file: + + source ~/.nix-profile/etc/profile.d/nix.sh + + Restart your terminal or run source ~/.bashrc or source ~/.zshrc to apply the changes. + +3. Install geckodriver using Nix: + + nix-env -iA nixpkgs.geckodriver + +### macOS + +1. Install the Nix package manager if you haven't already by running: + + curl -L https://nixos.org/nix/install | sh + +2. Add the following lines to your ~/.bashrc or ~/.zshrc file: + + source ~/.nix-profile/etc/profile.d/nix.sh + + Restart your terminal or run source ~/.bashrc or source ~/.zshrc to apply the changes. + +3. Install geckodriver using Nix: + + nix-env -iA nixpkgs.geckodriver + +## Running the script + +1. Make sure you have the script file test.py in your working directory. + +2. In the terminal, navigate to the directory containing the script and run: + + python test.py + +3. The script will open a Firefox browser window, navigate to the target website, and interact with the specified elements. The user will need to manually click the submit button when prompted. + +4. After the submit button is clicked, the script will continue to interact with the elements on the new page. + +## Extending the script + +To extend the script, you can add more interactions with the web page elements using the Selenium WebDriver API. Make sure to use the provided helper functions (wait_and_click, option_present_in_select, and select_option) to handle waiting for elements to appear and interacting with them. + +## Troubleshooting + +- If you encounter issues with the geckodriver path, make sure that the path is correctly set in the script, and that the geckodriver is installed in the specified location. + +- If you encounter issues with element interactions, make sure that the elements are present and visible on the web page, and that the script is using the correct element identifiers (e.g., IDs or class names diff --git a/samanman.code-workspace b/samanman.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/samanman.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..4de3715 --- /dev/null +++ b/shell.nix @@ -0,0 +1,37 @@ +with import {}; + +# geckodriver +# python38 +# python38Packages.selenium + +with import {}; + +let + python = let + packageOverrides = prev: final: { + selenium = final.selenium.overridePythonAttrs (old: { + src = fetchFromGitHub { + owner = "SeleniumHQ"; + repo = "selenium"; + rev = "refs/tags/selenium-4.8.0"; + hash = "sha256-YTi6SNtTWuEPlQ3PTeis9osvtnWmZ7SRQbne9fefdco="; + }; + postInstall = '' + install -Dm 755 ../rb/lib/selenium/webdriver/atoms/getAttribute.js $out/${python3Packages.python.sitePackages}/selenium/webdriver/remote/getAttribute.js + install -Dm 755 ../rb/lib/selenium/webdriver/atoms/isDisplayed.js $out/${python3Packages.python.sitePackages}/selenium/webdriver/remote/isDisplayed.js + ''; + }); + }; in python3.override { inherit packageOverrides; }; +in mkShell { + buildInputs = [ + (python.withPackages ( + ps: with ps; [ + pandas + requests + selenium + ] + )) + geckodriver + ]; +} + diff --git a/test.py b/test.py new file mode 100644 index 0000000..e225aa8 --- /dev/null +++ b/test.py @@ -0,0 +1,77 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.firefox.service import Service +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +import time +import os + +def main(): + # Define the target URL + url = "https://ir-appointment.visametric.com/en" + + # Get the geckodriver path and create a Firefox webdriver + geckodriver_path = os.environ.get('GECKODRIVER_PATH', '/nix/store/2nrjhmvqiirig1w3zhxs9mwhg1z9yprf-geckodriver-0.32.2/bin/geckodriver') + service = Service(executable_path=geckodriver_path) + driver = webdriver.Firefox(service=service) + + # Open the target URL + driver.get(url) + + try: + # Click the element with id "legalizationBtn" + wait_and_click(driver, By.ID, "legalizationBtn") + + # Click the element with id "result0" + wait_and_click(driver, By.ID, "result0") + + # Click the element with id "result1" + wait_and_click(driver, By.ID, "result1") + + # Wait for the user to manually click the submit button + wait = WebDriverWait(driver, 120) # Wait up to 120 seconds for user action + wait.until(lambda driver: driver.current_url == "https://ir-appointment.visametric.com/en/appointment-form") + + # Wait for the "ILAM" option to be present in the "city" select element and select it + wait.until(option_present_in_select(By.ID, "city", "ILAM")) + select_option(driver, By.ID, "city", "ILAM") + + # Wait for the "TEHRAN" option to be present in the "office" select element and select it + wait.until(option_present_in_select(By.ID, "office", "TEHRAN")) + select_option(driver, By.ID, "office", "TEHRAN") + + except Exception as e: + print("An error occurred:", e) + + finally: + time.sleep(60) # Wait for 60 seconds to see the result + driver.quit() # Close the browser + +def wait_and_click(driver, by, value): + # Wait for an element to be clickable and then click on it + wait = WebDriverWait(driver, 10) # Wait up to 10 seconds + element = wait.until(EC.element_to_be_clickable((by, value))) + element.click() + +def option_present_in_select(by, value, option_text): + # Check if an option is present in a select element + def _option_present(driver): + select_element = driver.find_element(by, value) + for option in select_element.find_elements(By.TAG_NAME, "option"): + if option.text == option_text: + return True + return False + + return _option_present + +def select_option(driver, by, value, option_text): + # Select an option in a select element by its text + select_element = driver.find_element(by, value) + for option in select_element.find_elements(By.TAG_NAME, "option"): + if option.text == option_text: + option.click() + break + +if __name__ == "__main__": + main()