Advertisement
notPlancha

Untitled

Mar 16th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as cond
  5. from selenium.webdriver.common.by import By
  6. from selenium.common.exceptions import NoSuchElementException
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.keys import Keys
  9. from selenium.webdriver.common.action_chains import ActionChains
  10.  
  11.  
  12. def wait_for_element(driver, max_time_waiting, xpath):
  13.     try:
  14.         WebDriverWait(driver, max_time_waiting).until(cond.presence_of_element_located((By.XPATH, xpath)))
  15.         return True
  16.     except TimeoutException:
  17.         return False
  18.  
  19.  
  20. def check_exists_by_xpath(driver, xpath):
  21.     try:
  22.         driver.find_element_by_xpath(xpath)
  23.         return True
  24.     except NoSuchElementException:
  25.         return False
  26.  
  27.  
  28. def open_new_tab(driver, url):
  29.     driver.execute_script("window.open('%s');" % url)
  30.  
  31.  
  32. def close_tab(driver):
  33.     driver.close()
  34.  
  35.  
  36. webdriver_path = "C:/Users/Andre/Desktop/python/files/chromedriver.exe"
  37. profile_path = "C:/Users/Andre/AppData/Local/Google/Chrome/User Data/"
  38. max_time_waiting = 10
  39.  
  40. options_chrome = webdriver.ChromeOptions()
  41. options_chrome.add_argument("--user-data-dir=" + profile_path)
  42. driver = webdriver.Chrome(webdriver_path, options=options_chrome)
  43.  
  44. url_aternos = "https://aternos.org/addons/list/%s/"
  45. xpath_mods = "//*[@class='plugin']"
  46. xpath_mod_name = "//*[@class='plugin-name']"
  47. xpath_title = "//*[@class='btn btn-invisible btn-clickme btn-small']"  # TODO verificar se está nas outras
  48. xpath_error = "//*[@class='error']"
  49.  
  50. n_url = 1 #Mudar sempre que para
  51. driver.get(url_aternos % str(n_url))
  52. wait_for_element(driver, 10, xpath_mods)
  53.  
  54. ciclo = True
  55. with open('C:/Users/Andre/Desktop/python/projects/Selenium/Mods/mods_result.txt', "a") as file:
  56.     file.write("1.15.2------------------------------------------------------------------------")#Mudar sempre que começa
  57.     while ciclo:
  58.         n_name = 0
  59.         urls = []
  60.         url_names = []
  61.         for element in driver.find_elements_by_xpath(xpath_mods):
  62.             urls.append(element.get_attribute("href"))
  63.             url_names.append(element.text)
  64.  
  65.         for url in urls:
  66.             driver.get(url)
  67.             wait_for_element(driver, 10, xpath_title)
  68.             if not check_exists_by_xpath(driver, xpath_error):
  69.                 try:
  70.                     file.write("\n" + url_names[n_name] + "\n")
  71.                 except UnicodeEncodeError:
  72.                     file.write("Unicode error, pag" + str(n_url))
  73.             n_name += 1
  74.         n_url += 1
  75.         driver.get(url_aternos % str(n_url))
  76.         ciclo = wait_for_element(driver, 10, xpath_mods)
  77.         time.sleep(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement