Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. package rpa;
  2.  
  3. import java.awt.AWTException;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.openqa.selenium.By;
  11. import org.openqa.selenium.JavascriptExecutor;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.WebElement;
  14. import org.openqa.selenium.chrome.ChromeDriver;
  15. import org.openqa.selenium.support.ui.ExpectedConditions;
  16. import org.openqa.selenium.support.ui.WebDriverWait;
  17.  
  18. public class Recolha {
  19.  
  20. public static final String USERNAME = "nunolopess@protonmail.com";
  21. public static final String PASSWORD = "password123";
  22. public static final String URL1 = "https://login.nos.pt/";
  23. public static final String URL2 = "https://www.vodafone.pt/";
  24. public static final int WAIT = 60;
  25. public static WebDriver driver;
  26.  
  27. // 303300565
  28. // bmrs motor de regras
  29. public static void main(String[] args) throws InterruptedException, AWTException {
  30.  
  31. IniciarDriver();
  32. nos();
  33. vodafone();
  34.  
  35. driver.quit(); // Fechar Browser
  36. }
  37.  
  38. public static void IniciarDriver() {
  39. System.setProperty("webdriver.chrome.driver",
  40. "C:\\Users\\WNDWS\\Downloads\\chromedriver_win32\\chromedriver.exe");
  41. driver = new ChromeDriver();
  42. // driver.manage().timeouts().pageLoadTimeout(WAIT, TimeUnit.SECONDS);
  43. }
  44.  
  45. public static void AbrirSite(String url) {
  46.  
  47. driver.get(url);
  48.  
  49. /*
  50. * try { Thread.sleep(WAIT); } catch (InterruptedException e) { // TODO
  51. * Auto-generated catch block e.printStackTrace(); } // Let the user actually
  52. * see something!
  53. */
  54. }
  55.  
  56. public static void PesquisarPorNome(String nome, String procura) {
  57.  
  58. WebElement searchBox = driver.findElement(By.name(nome));
  59. searchBox.clear();
  60. searchBox.sendKeys(procura);
  61. searchBox.submit();
  62.  
  63. }
  64.  
  65. public static void PesquisarPorXpath(String path, String procura) throws InterruptedException {
  66.  
  67. Esperar(path);
  68. WebElement searchBox = driver.findElement(By.xpath(path));
  69. searchBox.clear();
  70. searchBox.sendKeys(procura);
  71. // searchBox.submit();
  72. // driver.manage().timeouts().pageLoadTimeout(WAIT, TimeUnit.SECONDS);
  73.  
  74. }
  75.  
  76. public static void ClicarBotaoCss(String path) {
  77. WebElement button = driver.findElement(By.cssSelector(path));
  78. button.click();
  79. }
  80.  
  81. public static void ClicarBotaoXpath(String path) throws InterruptedException {
  82. Esperar(path);
  83. WebElement button = driver.findElement(By.xpath(path));
  84. button.click();
  85.  
  86. }
  87.  
  88. public static void LogIn(String pathUsername, String pathPassword, String buttonLogIn) throws InterruptedException {
  89.  
  90. Esperar(pathUsername);
  91. WebElement logInUser = driver.findElement(By.xpath(pathUsername));
  92. logInUser.clear();
  93. logInUser.sendKeys(USERNAME);
  94.  
  95. WebElement logInPass = driver.findElement(By.xpath(pathPassword));
  96. logInPass.clear();
  97. logInPass.sendKeys(PASSWORD);
  98.  
  99. WebElement button = driver.findElement(By.xpath(buttonLogIn));
  100. button.click();
  101.  
  102. }
  103.  
  104. public static void Esperar(String path) throws InterruptedException {
  105. WebDriverWait wait = new WebDriverWait(driver, WAIT);
  106. wait.until(ExpectedConditions.elementToBeClickable(By.xpath(path)));
  107. Thread.sleep(1000);
  108. }
  109.  
  110. public static Boolean Present(String path) { // true Elemento presente false Elemento não presente
  111.  
  112. try {
  113. driver.findElement(By.xpath(path)).isEnabled();
  114. return true;
  115. } catch (org.openqa.selenium.NoSuchElementException e) {
  116. return false;
  117. }
  118. }
  119.  
  120. public static void WaitForAjax() throws InterruptedException {
  121.  
  122. while (true) {
  123.  
  124. Boolean ajaxIsComplete = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0");
  125. if (ajaxIsComplete) {
  126. break;
  127. }
  128. Thread.sleep(100);
  129. }
  130. }
  131.  
  132. public static void WaitForJavascript() {
  133. JavascriptExecutor js = (JavascriptExecutor) driver;
  134.  
  135. // Initially bellow given if condition will check ready state of page.
  136. if (js.executeScript("return document.readyState").toString().equals("complete")) {
  137. // System.out.println("Page Is loaded.");
  138. return;
  139. }
  140.  
  141. // This loop will rotate for 25 times to check If page Is ready after every 1
  142. // second.
  143. // You can replace your value with 25 If you wants to Increase or decrease wait
  144. // time.
  145. for (int i = 0; i < 60; i++) {
  146. try {
  147. Thread.sleep(1000);
  148. } catch (InterruptedException e) {
  149. }
  150. // To check page ready state.
  151. if (js.executeScript("return document.readyState").toString().equals("complete")) {
  152. break;
  153. }
  154. }
  155.  
  156. }
  157.  
  158. private static List<String> readFile(String filename) {
  159. List<String> records = new ArrayList<String>();
  160. try {
  161. BufferedReader reader = new BufferedReader(new FileReader(filename));
  162. String line;
  163. while ((line = reader.readLine()) != null) {
  164. records.add(line);
  165. }
  166. reader.close();
  167. return records;
  168. } catch (Exception e) {
  169. System.err.format("Exception occurred trying to read '%s'.", filename);
  170. e.printStackTrace();
  171. return null;
  172. }
  173. }
  174.  
  175. public static void vodafone() throws InterruptedException, AWTException {
  176. AbrirSite(URL2);
  177.  
  178. // FAZER LOG IN
  179. LogIn("//*[@id=\"ql_username\"]", "//*[@id=\"ql_password\"]", "//*[@id=\"btnLoginHome\"]");
  180.  
  181. Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div[1]/div[1]/div[2]/div[3]/a[1]");
  182. WaitForJavascript();
  183. WaitForAjax();
  184.  
  185. List<String> telefones = readFile("./src/vodafone.txt");
  186. String principal = telefones.get(0);
  187. PesquisarPorXpath("//*[@id=\"__ns2036994902_sfItemName\"]", principal);
  188. String relatoriosURL = driver.getCurrentUrl(); // Relatórios
  189.  
  190. Esperar("//*[@id=\"__ns2036994902_searchBtn\"]");
  191.  
  192. ClicarBotaoXpath("//*[@id=\"__ns2036994902_searchBtn\"]");
  193.  
  194. Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
  195.  
  196. ClicarBotaoXpath("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
  197.  
  198. ClicarBotaoXpath("//*[@id=\"__ns2036993941_communicationsaction\"]");
  199.  
  200. Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
  201.  
  202. WaitForJavascript();
  203. WaitForAjax();
  204.  
  205. for (int i = 1; i < telefones.size(); i++) {
  206. String numero = telefones.get(i);
  207. PesquisarPorXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/input", numero);
  208. Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
  209. WaitForJavascript();
  210. WaitForAjax();
  211.  
  212. ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
  213. WaitForJavascript();
  214. WaitForAjax();
  215. Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
  216.  
  217. ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
  218. WaitForJavascript();
  219. WaitForAjax();
  220.  
  221. }
  222. ClicarBotaoXpath("//*[@id=\"__ns2036993941_continueBtn\"]");
  223. ClicarBotaoXpath("//*[@id=\"__ns2036993941_billingPeriodItem1\"]");
  224. ClicarBotaoXpath("//*[@id=\"__ns2036993941_exportBtn\"]");
  225. int antes = dirFileCount("C:\\Users\\WNDWS\\Downloads\\");
  226. Esperar("//*[@id=\"__ns2036993941_downloadLnk\"]");// link de download
  227.  
  228. ClicarBotaoXpath("//*[@id=\"__ns2036993941_downloadLnk\"]");
  229. while (antes == dirFileCount("C:\\Users\\WNDWS\\Downloads\\")) {
  230. Thread.sleep(2000);
  231. }
  232.  
  233. Thread.sleep(5000);
  234.  
  235. AbrirSite(relatoriosURL);
  236.  
  237. Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div[1]/div[1]/div[2]/div[3]/a[1]");
  238. WaitForJavascript();
  239. WaitForAjax();
  240. PesquisarPorXpath("//*[@id=\"__ns2036994902_sfItemName\"]", principal);
  241.  
  242. Esperar("//*[@id=\"__ns2036994902_searchBtn\"]");
  243.  
  244. ClicarBotaoXpath("//*[@id=\"__ns2036994902_searchBtn\"]");
  245.  
  246. Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
  247.  
  248. ClicarBotaoXpath("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
  249.  
  250. ClicarBotaoXpath("//*[@id=\"__ns2036993941_currentChargesaction\"]");
  251.  
  252. for (int i = 1; i < telefones.size(); i++) {
  253. String numero = telefones.get(i);
  254. PesquisarPorXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/input", numero);
  255. Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
  256. WaitForJavascript();
  257. WaitForAjax();
  258.  
  259. ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
  260. WaitForJavascript();
  261. WaitForAjax();
  262. Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
  263.  
  264. ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
  265. WaitForJavascript();
  266. WaitForAjax();
  267.  
  268. }
  269. ClicarBotaoXpath("//*[@id=\"__ns2036993941_continueBtn\"]");
  270. ClicarBotaoXpath("//*[@id=\"__ns2036993941_billingPeriodItem1\"]");
  271. ClicarBotaoXpath("//*[@id=\"__ns2036993941_exportBtn\"]");
  272. antes = dirFileCount("C:\\Users\\WNDWS\\Downloads\\");
  273. Esperar("//*[@id=\"__ns2036993941_downloadLnk\"]");// link de download
  274.  
  275. ClicarBotaoXpath("//*[@id=\"__ns2036993941_downloadLnk\"]");
  276. while (antes == dirFileCount("C:\\Users\\WNDWS\\Downloads\\")) {
  277. Thread.sleep(2000);
  278. }
  279. Thread.sleep(5000);
  280.  
  281. }
  282.  
  283. public static int dirFileCount(String path) {
  284.  
  285. File directory = new File(path);
  286. int filecount = directory.list().length;
  287. return filecount;
  288.  
  289. }
  290.  
  291. public static void nos() throws InterruptedException {
  292. AbrirSite(URL1);
  293.  
  294. // FAZER LOG IN
  295. LogIn("//*[@id=\"Username\"]", "//*[@id=\"Password\"]", "/html/body/div[2]/form/button");
  296. ClicarBotaoXpath("/html/body/div[2]/div/div[2]/div/section[3]/a");
  297.  
  298. // String elemento1 =
  299. // "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section[";
  300. // String elemento2 = "]/div/div/div/div[3]/div/button";
  301. // String elem3 =
  302. // "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section[";
  303. // String elem4 = "]/div/div/div/div[3]/div/div/ul/li[2]/a";
  304.  
  305. // MUDAR A TAB
  306. ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
  307. driver.switchTo().window(tabs2.get(1));
  308.  
  309. String servicosURL = driver.getCurrentUrl(); // GERIR SERVIÇOS
  310. List<String> contas = readFile("./src/nos.txt");
  311.  
  312. for (int i = 0; i < contas.size(); i++) {
  313. String conta = contas.get(i);
  314. PesquisarPorXpath("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/input",
  315. conta);
  316. Esperar("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/span/i");
  317. Thread.sleep(2000);
  318. ClicarBotaoXpath("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/span/i");
  319. WaitForJavascript();
  320. WaitForAjax();
  321. Esperar("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section/div/div/div/div[3]/div/button");
  322. ClicarBotaoXpath(
  323. "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section/div/div/div/div[3]/div/button");
  324. ClicarBotaoXpath(
  325. "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section/div/div/div/div[3]/div/div/ul/li[2]/a");
  326.  
  327. WaitForJavascript();
  328. WaitForAjax();
  329. Boolean faturas = Present(
  330. "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/button");
  331.  
  332. if (faturas) {
  333. ClicarBotaoXpath(
  334. "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/button");
  335. ClicarBotaoXpath(
  336. "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/div/ul/li[3]/a");
  337. }
  338. AbrirSite(servicosURL);
  339.  
  340. }
  341.  
  342. /*
  343. * Boolean contas = false; int i = 1; String valor; valor = Integer.toString(i);
  344. * do {
  345. *
  346. * ClicarBotaoXpath(elemento1 + valor + elemento2);
  347. *
  348. * ClicarBotaoXpath(elem3 + valor + elem4);
  349. *
  350. * WaitForAjax();
  351. *
  352. * contas = Present(
  353. * "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/button"
  354. * );
  355. *
  356. * if (contas) { ClicarBotaoXpath(
  357. * "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/button"
  358. * ); ClicarBotaoXpath(
  359. * "/html/body/div[1]/main/div/div[2]/div[2]/div/div/section/div/div/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[3]/div/div/ul/li[3]/a"
  360. * ); } AbrirSite(servicosURL); i++; valor = Integer.toString(i); contas =
  361. * false;
  362. *
  363. * } while (!driver.findElements(By.xpath(elemento1 + valor +
  364. * elemento2)).isEmpty());
  365. */
  366.  
  367. // LOG OUT
  368. ClicarBotaoXpath("//*[@id=\"HeaderPlaceHolder\"]/div[2]/div/div/div/div[4]/div/div/button/span[2]");
  369. ClicarBotaoXpath("//*[@id=\"HeaderPlaceHolder\"]/div[2]/div/div/div/div[4]/div/div/div/a");
  370.  
  371. }
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement