gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Make this script. The script needs to perform the following functions: It needs to solve…
- User
Make this script. The script needs to perform the following functions:
It needs to solve simple ONE letter captcha.
- Open a browser, in my case chrome.
- Access the site "narutoplayers.com.br/np2/?p=login", but DO NOT LOGIN, WAIT UNTIL THE USER LOGINS.
- Wait until the user is on the "https://www.narutoplayers.com.br/np2/?p=news" page.
- Once logged in, click on the text "Hunts" to be redirected to another page.
- Search for a specific field to make a select.
- Select the text containing the word "Student".
- Click to search.
- Read the image that has a text and write the letter that the text contains in a specific field just below the image.
- Click to search again.
- The program should have a timer of 300s.
- The program should repeat all actions, except for opening the browser and logging in.
- The browser should not be closed in this process.
Thanks
- Assistant
in advance!
Here's the script:
from time import time, sleep from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from PIL import Image import pytesseract # Define the URL and desired field text url = "https://www.narutoplayers.com.br/np2/?p=hunts" field_text = "Student" # Define the function to solve the captcha def solve_captcha(driver): captcha = driver.find_element_by_xpath("//div[@class='captcha']/img") captcha_location = captcha.location captcha_size = captcha.size captcha_screenshot = driver.get_screenshot_as_png() captcha_image = Image.open(io.BytesIO(captcha_screenshot)) captcha_left = captcha_location['x'] captcha_top = captcha_location['y'] captcha_right = captcha_location['x'] + captcha_size['width'] captcha_bottom = captcha_location['y'] + captcha_size['height'] captcha_image = captcha_image.crop((captcha_left, captcha_top, captcha_right, captcha_bottom)) captcha_text = pytesseract.image_to_string(captcha_image) captcha_input = driver.find_element_by_xpath("//input[@name='txtCaptcha']") captcha_input.send_keys(captcha_text) # Define the function to repeat the actions def repeat_actions(driver): while True: # Navigate to the Hunts page driver.get(url) # Wait until the page is loaded and the desired element is visible hunts_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, "Hunts"))) # Click on the "Hunts" button hunts_button.click() # Wait for the page to load and the desired element to become selectable field_select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "selFiltro04"))) # Select the desired field options = field_select.find_elements_by_tag_name("option") for option in options: if field_text in option.text: option.click() break # Click on the "Search" button search_button = driver.find_element_by_xpath("//input[@type='submit'][@value='Search']") search_button.click() # Solve the captcha solve_captcha(driver) # Click on the "Search" button again search_button.click() # Wait for the timer to expire before repeating the actions sleep(300) # Define the main function def main(): # Open the browser driver = webdriver.Chrome() # Go to the login page and wait for the user to log in driver.get("https://www.narutoplayers.com.br/np2/?p=login") WebDriverWait(driver, 0).until(EC.url_contains("narutoplayers.com.br/np2/?p=news")) # Repeat the actions indefinitely repeat_actions(driver) # Call the main function if __name__ == "__main__": main() - User
Do it again, but with new solutions.
- Assistant
Sure, here's another solution:
from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from PIL import Image import pytesseract # Define the URL and desired field text url = "https://www.narutoplayers.com.br/np2/?p=hunts" field_text = "Student" # Define the function to solve the captcha def solve_captcha(driver): captcha = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "captcha"))) captcha_location = captcha.location captcha_size = captcha.size captcha_screenshot = driver.get_screenshot_as_png() captcha_image = Image.open(io.BytesIO(captcha_screenshot)) captcha_left = captcha_location['x'] captcha_top = captcha_location['y'] captcha_right = captcha_location['x'] + captcha_size['width'] captcha_bottom = captcha_location['y'] + captcha_size['height'] captcha_image = captcha_image.crop((captcha_left, captcha_top, captcha_right, captcha_bottom)) captcha_text = pytesseract.image_to_string(captcha_image) captcha_input = driver.find_element_by_name("txtCaptcha") captcha_input.clear() captcha_input.send_keys(captcha_text) # Define the function to repeat the actions def repeat_actions(driver): while True: # Navigate to the Hunts page driver.get(url) # Wait until the page is loaded and the "Hunts" button is clickable hunts_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Hunts"))) # Click on the "Hunts" button hunts_button.click() # Wait for the page to load and the select element to be selectable field_select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "selFiltro04"))) # Select the desired field text options = field_select.find_elements_by_tag_name("option") for option in options: if field_text in option.text: option.click() break # Click on the "Search" button search_button = driver.find_element_by_css_selector("input[type='submit'][value='Search']") search_button.click() # Solve the captcha solve_captcha(driver) # Click on the "Search" button again search_button.click() # Wait for the timer to expire before repeating the actions sleep(300) # Define the main function def main(): # Open the browser driver = webdriver.Chrome() # Go to the login page and wait for the user to log in driver.get("https://www.narutoplayers.com.br/np2/?p=login") WebDriverWait(driver, 0).until(EC.url_contains("narutoplayers.com.br/np2/?p=news")) # Repeat the actions indefinitely repeat_actions(driver) # Call the main function if __name__ == "__main__": main()This script uses similar logic to the previous one, with some improvements, such as using the wait conditions more effectively for more reliable performance. It also clears the captcha input before entering the solved text, to avoid appending to previous text if the field already had a value.