initial commit

This commit is contained in:
Arne Weiss
2023-04-11 21:59:54 +02:00
commit 73a68c74c0
5 changed files with 199 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
geckodriver.log

76
Readme.md Normal file
View File

@@ -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:
<code>curl -L https://nixos.org/nix/install | sh</code>
2. Add the following lines to your <code>~/.bashrc</code> or <code>~/.zshrc</code> file:
<code>source ~/.nix-profile/etc/profile.d/nix.sh</code>
Restart your terminal or run <code>source ~/.bashrc</code> or <code>source ~/.zshrc</code> to apply the changes.
3. Install geckodriver using Nix:
<code>nix-env -iA nixpkgs.geckodriver</code>
### macOS
1. Install the Nix package manager if you haven't already by running:
<code>curl -L https://nixos.org/nix/install | sh</code>
2. Add the following lines to your <code>~/.bashrc</code> or <code>~/.zshrc</code> file:
<code>source ~/.nix-profile/etc/profile.d/nix.sh</code>
Restart your terminal or run <code>source ~/.bashrc</code> or <code>source ~/.zshrc</code> to apply the changes.
3. Install geckodriver using Nix:
<code>nix-env -iA nixpkgs.geckodriver</code>
## Running the script
1. Make sure you have the script file <code>test.py</code> in your working directory.
2. In the terminal, navigate to the directory containing the script and run:
<code>python test.py</code>
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 (<code>wait_and_click</code>, <code>option_present_in_select</code>, and <code>select_option</code>) 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

8
samanman.code-workspace Normal file
View File

@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

37
shell.nix Normal file
View File

@@ -0,0 +1,37 @@
with import <nixpkgs-unstable> {};
# geckodriver
# python38
# python38Packages.selenium
with import <nixpkgs> {};
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
];
}

77
test.py Normal file
View File

@@ -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()