-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31ddea4
commit e06f64e
Showing
1 changed file
with
57 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,79 @@ | ||
# 270toWin Election Simulation Automation | ||
# 270toWin Simulation Automation | ||
|
||
This Python script automates running simulations on the [270toWin 2024 Election Simulation](https://www.270towin.com/2024-simulation/) website. It sets the simulation speed, runs the simulation a specified number of times, and tracks the results for each run. Throughout the process, it prints out information on which candidate won each simulation. | ||
This project automates the 270toWin 2024 Election Simulation to record simulation outcomes and provide summary statistics. The script uses Selenium to interact with the website and execute multiple simulations, logging each outcome. | ||
|
||
## Features | ||
- Automatically set the simulation speed on the 270toWin website. | ||
- Run the simulation a specified number of times. | ||
- Track how many times each candidate wins or ties during the simulation. | ||
- Print results for each simulation run, including which candidate won and the running percentage of wins. | ||
## Requirements | ||
|
||
## Prerequisites | ||
Before running the script, make sure you have the following installed: | ||
- Python 3.7+ | ||
- Selenium (`pip install selenium`) | ||
- Webdriver Manager (`pip install webdriver-manager`) | ||
|
||
- **Python 3.7 or higher**: You can download it from [python.org](https://www.python.org/downloads/). | ||
- **Google Chrome**: The script uses the Chrome browser to run the automation. | ||
- **ChromeDriver**: Managed automatically by `webdriver-manager`. | ||
- **Selenium**: To install Selenium, run: | ||
```bash | ||
pip install selenium webdriver-manager | ||
``` | ||
## Setup | ||
|
||
## Setup Instructions | ||
1. **Install Dependencies**: | ||
``` | ||
pip install selenium webdriver-manager | ||
``` | ||
|
||
### Step 1: Clone the Repository | ||
To download this script, clone the repository using: | ||
```bash | ||
git clone https://github.com/thejessicafelts/270towin-simulation-automation.git | ||
cd 270towin-simulation-automation | ||
``` | ||
2. **WebDriver**: | ||
- The script uses Chrome WebDriver, which is managed automatically by WebDriver Manager. | ||
|
||
### Step 2: Install Required Packages | ||
Make sure you have `selenium` and `webdriver-manager` installed: | ||
```bash | ||
pip install selenium webdriver-manager | ||
``` | ||
## Usage | ||
|
||
### Step 3: Preventing Your Computer from Sleeping During the Simulation | ||
- **For Mac Users**: Use the `caffeinate` command to prevent your Mac from going to sleep while the script runs: | ||
```bash | ||
caffeinate -i python3 script_name.py | ||
``` | ||
Replace `script_name.py` with the actual name of your Python script. The `-i` option ensures your Mac stays awake as long as the script is running. | ||
|
||
- **For Windows Users**: Adjust your power settings to prevent the computer from sleeping: | ||
1. Go to **Settings > System > Power & Sleep**. | ||
2. Under **Sleep**, set **"When plugged in, PC goes to sleep after"** to **"Never."** | ||
3. Alternatively, you can use third-party tools to keep your system awake, such as `Caffeine.exe` or `NoSleep.exe`. | ||
|
||
## Running the Script | ||
|
||
### Step 1: Modify Configuration (Optional) | ||
Edit the following variables in the script if you need to adjust the settings: | ||
- **`url`**: The URL of the 270toWin simulation page (default is set correctly). | ||
- **`sim_speed_id`**: The ID for the simulation speed button (default: `sim_speed_4`). | ||
- **`simulation_count`**: Number of times to run the simulation (default: `1000`). | ||
- **`wait_time`**: Time to wait (in seconds) between simulations (default: `6`). | ||
|
||
### Step 2: Run the Script | ||
Execute the script using: | ||
- **Mac**: | ||
```bash | ||
caffeinate -i python3 script_name.py | ||
``` | ||
- **Windows**: | ||
```bash | ||
python script_name.py | ||
``` | ||
Run the `automation.py` script to execute the simulation: | ||
|
||
### Example Output | ||
```plaintext | ||
-------------------------------------------------------------------------------- | ||
Simulation 1: Democrats Win. | ||
---------- | ||
Democrats: 320, Republicans: 218 | ||
Democrats have won 100.00% of simulations. (1 out of 1) | ||
-------------------------------------------------------------------------------- | ||
Simulation 2: Republicans Win. | ||
---------- | ||
Democrats: 249, Republicans: 289 | ||
Republicans have won 50.00% of simulations. (1 out of 2) | ||
-------------------------------------------------------------------------------- | ||
Simulation 3: Democrats and Republicans have Tied. | ||
---------- | ||
Democrats: 269, Republicans: 269 | ||
50.00% of Simulations (1 of 3) have resulted in a Tie. | ||
-------------------------------------------------------------------------------- | ||
```bash | ||
python automation.py | ||
``` | ||
|
||
## Code Walkthrough | ||
### Configuration | ||
|
||
1. **Initialization**: | ||
- The script sets up the WebDriver using ChromeDriver and `webdriver-manager`. | ||
- The simulation speed is set by finding and clicking the appropriate button on the website. | ||
The following parameters can be configured directly in the script: | ||
|
||
2. **Simulation Loop**: | ||
- The script runs the simulation the specified number of times. | ||
- After each simulation run, it waits for the results to appear and retrieves them using the designated HTML element IDs. | ||
- The results are converted to integers, and the script tracks how many times each candidate wins or if there's a tie. | ||
- `simulation_count`: Sets the number of simulations to run (default: 100). | ||
- `wait_time`: The time to wait after each simulation is triggered, allowing it to complete (default: 6 seconds). | ||
|
||
3. **Result Calculation & Display**: | ||
- The script calculates and prints the win percentage for Democrats and Republicans, as well as the number of ties, after each simulation run. | ||
### Simulation Execution | ||
|
||
## Future Enhancements | ||
1. **Load the 270toWin Simulation Page**: | ||
The script opens the 270toWin 2024 simulation page and sets the simulation speed to the maximum (ID: `sim_speed_4`). | ||
|
||
- **Final Summary**: Add a final summary that prints the overall win percentages for Democrats, Republicans, and ties at the end of all simulations. | ||
- **Robust Error Handling**: Implement more error handling to gracefully handle issues, such as network interruptions or missing elements on the page. | ||
- **Command-Line Arguments**: Allow users to specify `simulation_count`, `wait_time`, and other configurations via command-line arguments for easier usage. | ||
2. **Run Simulations**: | ||
- For each simulation run: | ||
- Click the "Run Simulation" button to start. | ||
- Wait for the simulation to complete. | ||
- Retrieve results for Harris and Trump from the page. | ||
- Update win counters and percentages based on the retrieved values. | ||
|
||
## Troubleshooting | ||
3. **Output Summary**: | ||
- For each simulation, the script prints: | ||
- The simulation result (winner: Harris, Trump, or Tie). | ||
- Counts and win percentages for Harris, Trump, and Tie outcomes. | ||
|
||
- **ChromeDriver Issues**: | ||
- Ensure that your Chrome browser is up to date. `webdriver-manager` should handle most ChromeDriver version issues, but sometimes mismatches can occur. Updating Chrome typically resolves these. | ||
4. **Final Summary**: | ||
After all simulations are completed, the script provides a cumulative summary of Harris and Trump’s wins and the percentage of simulations that ended in a tie. | ||
|
||
- **Permissions Errors**: | ||
- If you encounter permissions issues when running the script on Windows, try running the command prompt as an Administrator. | ||
### Example Output | ||
|
||
- **System Goes to Sleep**: | ||
- Make sure to use the `caffeinate` command on Mac or adjust the power settings on Windows as described above. | ||
``` | ||
-------------------------------------------------------------------------------- | ||
Simulation 1: Harris Wins. | ||
---------- | ||
Harris: 278, Trump: 260 | ||
Harris has won 100.00% of simulations. (1 out of 1) | ||
... | ||
-------------------------------------------------------------------------------- | ||
Simulation 100: Trump Wins. | ||
---------- | ||
Harris: 248, Trump: 290 | ||
Trump has won 55.00% of simulations. (55 out of 100) | ||
``` | ||
|
||
## Contributing | ||
### Notes | ||
|
||
Feel free to submit issues or pull requests if you'd like to contribute to improving the script. Make sure to follow proper coding standards and provide thorough explanations for any changes made. | ||
- **Simulation Speed**: The script sets the simulation speed to the maximum setting available on the page to minimize runtime. | ||
- **Browser Window**: The `finally` block is commented out to keep the browser open after the script finishes. Uncomment it to close the browser automatically after completion: | ||
```python | ||
finally: | ||
driver.quit() | ||
``` |