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(THE CAPTCHA IS IN THE CAPTCHA.PHP).
- Click to search again.
- The program should have a timer of 300s.
- The program should repeat all actions, except for opening the browser.
- The browser should not be closed in this process.
Thanks
- Assistant
in advance.
Here's the script to perform the functions you specified:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep
- User
Refactor the code and leave it in the requested execution order.
- Assistant
Sure, here's the refactored code with the functions in the requested execution order:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time #function to read the captcha image def read_captcha_image(image_url): #implement image recognition code here to read the captcha image and return the text pass #function to solve the captcha def solve_captcha(image_field, captcha_field): #read the captcha image and get the text captcha_image = image_field.get_attribute('src') captcha_text = read_captcha_image(captcha_image) #enter the captcha text in the captcha field captcha_field.send_keys(captcha_text) #click to search again search_button = driver.find_element_by_css_selector('input[name="submit"]') search_button.click() #open chrome browser driver = webdriver.Chrome() #access the website and wait for user to login driver.get("https://narutoplayers.com.br/np2/?p=login") input("Please login and press Enter to continue...") #wait for the user to be redirected to the news page while True: if driver.current_url == "https://www.narutoplayers.com.br/np2/?p=news": break else: time.sleep(1) #click on the 'Hunts' link to go to another page hunts_link = driver.find_element_by_css_selector('a[href="#OtherHunts"]') hunts_link.click() #select the 'Student' option and click to search select_field = driver.find_element_by_css_selector('select[name="sortby"]') option_student = select_field.find_element_by_xpath("//option[contains(text(), 'Student')]") option_student.click() search_button = driver.find_element_by_css_selector('input[name="submit"]') search_button.click() #solve the captcha and repeat the process for 5 minutes start_time = time.time() while time.time() - start_time <= 300: #wait for page to load and find the captcha image and field time.sleep(1) captcha_image = driver.find_element_by_css_selector('img[src*="captcha.php"]') captcha_field = driver.find_element_by_css_selector('input[name="security"]') #solve the captcha and continue solve_captcha(captcha_image, captcha_field) #close the browser driver.close()