Here's the complete script divided in separate functions as suggested. This script uses Python with Selenium webdriver.
Note: Please make sure you have the following packages installed: selenium and webdriver_manager.
To install these packages, use the following commands:
pip install selenium
pip install webdriver_manager
Script:
import time
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 webdriver_manager.chrome import ChromeDriverManager
def start_browser():
driver = webdriver.Chrome(ChromeDriverManager().install())
return driver
def open_page(driver):
driver.get("http://narutoplayers.com.br/np2/?p=login")
return driver
def wait_for_login(driver):
WebDriverWait(driver, 300).until(EC.url_changes("http://narutoplayers.com.br/np2/?p=login"))
return driver
def go_to_hunts(driver):
driver.get("https://www.narutoplayers.com.br/np2/?p=news")
hunts_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "Hunts")))
hunts_link.click()
return driver
def select_student(driver):
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "mySelect")))
select = driver.find_element_by_id("mySelect")
for option in select.find_elements_by_tag_name("option"):
if "Student" in option.text:
option.click()
break
return driver
def click_search(driver):
search_button = driver.find_element_by_xpath("//input[@type='submit' and @value='Search']")
search_button.click()
return driver
def read_and_input_captcha(driver):
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "captcha")))
captcha = driver.find_element_by_id("captcha")
captcha_text = captcha.get_attribute("src")[-5]
driver.find_element_by_id("captchaInput").send_keys(captcha_text)
return driver
def search_again(driver):
time.sleep(300)
click_search(driver)
return driver
def main():
driver = start_browser()
open_page(driver)
wait_for_login(driver)
while True:
go_to_hunts(driver)
select_student(driver)
click_search(driver)
read_and_input_captcha(driver)
search_again(driver)
if __name__ == "__main__":
main()
This script has 10 functions:
start_browser(): Starts a Chrome browser
open_page(driver): Opens the specified page
wait_for_login(driver): Waits until the user logs in
go_to_hunts(driver): Goes to the "Hunts" page
select_student(driver): Selects the option containing the word "Student"
click_search(driver): Clicks the search button
read_and_input_captcha(driver): Reads the captcha image and inputs the letter into the field
search_again(driver): Waits for 300 seconds and clicks search again
main(): Controller function that calls the other functions in order
- And the last function call,
if __name__ == "__main__":, runs the main() function