Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rpa;
- import java.awt.AWTException;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- public class Recolha {
- public static final String USERNAME = "nunolopess@protonmail.com";
- public static final String PASSWORD = "password123";
- public static final String URL1 = "https://login.nos.pt/";
- public static final String URL2 = "https://www.vodafone.pt/";
- public static final int WAIT = 60;
- public static WebDriver driver;
- // 303300565
- // bmrs motor de regras
- public static void main(String[] args) throws InterruptedException, AWTException {
- IniciarDriver();
- nos();
- vodafone();
- driver.quit(); // Fechar Browser
- }
- public static void IniciarDriver() {
- System.setProperty("webdriver.chrome.driver",
- "C:\\Users\\WNDWS\\Downloads\\chromedriver_win32\\chromedriver.exe");
- driver = new ChromeDriver();
- // driver.manage().timeouts().pageLoadTimeout(WAIT, TimeUnit.SECONDS);
- }
- public static void AbrirSite(String url) {
- driver.get(url);
- /*
- * try { Thread.sleep(WAIT); } catch (InterruptedException e) { // TODO
- * Auto-generated catch block e.printStackTrace(); } // Let the user actually
- * see something!
- */
- }
- public static void PesquisarPorNome(String nome, String procura) {
- WebElement searchBox = driver.findElement(By.name(nome));
- searchBox.clear();
- searchBox.sendKeys(procura);
- searchBox.submit();
- }
- public static void PesquisarPorXpath(String path, String procura) throws InterruptedException {
- Esperar(path);
- WebElement searchBox = driver.findElement(By.xpath(path));
- searchBox.clear();
- searchBox.sendKeys(procura);
- // searchBox.submit();
- // driver.manage().timeouts().pageLoadTimeout(WAIT, TimeUnit.SECONDS);
- }
- public static void ClicarBotaoCss(String path) {
- WebElement button = driver.findElement(By.cssSelector(path));
- button.click();
- }
- public static void ClicarBotaoXpath(String path) throws InterruptedException {
- Esperar(path);
- WebElement button = driver.findElement(By.xpath(path));
- button.click();
- }
- public static void LogIn(String pathUsername, String pathPassword, String buttonLogIn) throws InterruptedException {
- Esperar(pathUsername);
- WebElement logInUser = driver.findElement(By.xpath(pathUsername));
- logInUser.clear();
- logInUser.sendKeys(USERNAME);
- WebElement logInPass = driver.findElement(By.xpath(pathPassword));
- logInPass.clear();
- logInPass.sendKeys(PASSWORD);
- WebElement button = driver.findElement(By.xpath(buttonLogIn));
- button.click();
- }
- public static void Esperar(String path) throws InterruptedException {
- WebDriverWait wait = new WebDriverWait(driver, WAIT);
- wait.until(ExpectedConditions.elementToBeClickable(By.xpath(path)));
- Thread.sleep(1000);
- }
- public static Boolean Present(String path) { // true Elemento presente false Elemento não presente
- try {
- driver.findElement(By.xpath(path)).isEnabled();
- return true;
- } catch (org.openqa.selenium.NoSuchElementException e) {
- return false;
- }
- }
- public static void WaitForAjax() throws InterruptedException {
- while (true) {
- Boolean ajaxIsComplete = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0");
- if (ajaxIsComplete) {
- break;
- }
- Thread.sleep(100);
- }
- }
- public static void WaitForJavascript() {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- // Initially bellow given if condition will check ready state of page.
- if (js.executeScript("return document.readyState").toString().equals("complete")) {
- // System.out.println("Page Is loaded.");
- return;
- }
- // This loop will rotate for 25 times to check If page Is ready after every 1
- // second.
- // You can replace your value with 25 If you wants to Increase or decrease wait
- // time.
- for (int i = 0; i < 60; i++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- // To check page ready state.
- if (js.executeScript("return document.readyState").toString().equals("complete")) {
- break;
- }
- }
- }
- private static List<String> readFile(String filename) {
- List<String> records = new ArrayList<String>();
- try {
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- String line;
- while ((line = reader.readLine()) != null) {
- records.add(line);
- }
- reader.close();
- return records;
- } catch (Exception e) {
- System.err.format("Exception occurred trying to read '%s'.", filename);
- e.printStackTrace();
- return null;
- }
- }
- public static void vodafone() throws InterruptedException, AWTException {
- AbrirSite(URL2);
- // FAZER LOG IN
- LogIn("//*[@id=\"ql_username\"]", "//*[@id=\"ql_password\"]", "//*[@id=\"btnLoginHome\"]");
- Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div[1]/div[1]/div[2]/div[3]/a[1]");
- WaitForJavascript();
- WaitForAjax();
- List<String> telefones = readFile("./src/vodafone.txt");
- String principal = telefones.get(0);
- PesquisarPorXpath("//*[@id=\"__ns2036994902_sfItemName\"]", principal);
- String relatoriosURL = driver.getCurrentUrl(); // Relatórios
- Esperar("//*[@id=\"__ns2036994902_searchBtn\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036994902_searchBtn\"]");
- Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_communicationsaction\"]");
- Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
- WaitForJavascript();
- WaitForAjax();
- for (int i = 1; i < telefones.size(); i++) {
- String numero = telefones.get(i);
- PesquisarPorXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/input", numero);
- Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
- WaitForJavascript();
- WaitForAjax();
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
- WaitForJavascript();
- WaitForAjax();
- Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
- WaitForJavascript();
- WaitForAjax();
- }
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_continueBtn\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_billingPeriodItem1\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_exportBtn\"]");
- int antes = dirFileCount("C:\\Users\\WNDWS\\Downloads\\");
- Esperar("//*[@id=\"__ns2036993941_downloadLnk\"]");// link de download
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_downloadLnk\"]");
- while (antes == dirFileCount("C:\\Users\\WNDWS\\Downloads\\")) {
- Thread.sleep(2000);
- }
- Thread.sleep(5000);
- AbrirSite(relatoriosURL);
- Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div[1]/div[1]/div[2]/div[3]/a[1]");
- WaitForJavascript();
- WaitForAjax();
- PesquisarPorXpath("//*[@id=\"__ns2036994902_sfItemName\"]", principal);
- Esperar("//*[@id=\"__ns2036994902_searchBtn\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036994902_searchBtn\"]");
- Esperar("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036994902_portfolioItems\"]/div/div[1]/div[2]/div[3]/a[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_currentChargesaction\"]");
- for (int i = 1; i < telefones.size(); i++) {
- String numero = telefones.get(i);
- PesquisarPorXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/input", numero);
- Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
- WaitForJavascript();
- WaitForAjax();
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/div[2]/div[1]/a");
- WaitForJavascript();
- WaitForAjax();
- Esperar("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_tree\"]/div[2]/div/div[2]/ul/li/div[1]");
- WaitForJavascript();
- WaitForAjax();
- }
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_continueBtn\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_billingPeriodItem1\"]");
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_exportBtn\"]");
- antes = dirFileCount("C:\\Users\\WNDWS\\Downloads\\");
- Esperar("//*[@id=\"__ns2036993941_downloadLnk\"]");// link de download
- ClicarBotaoXpath("//*[@id=\"__ns2036993941_downloadLnk\"]");
- while (antes == dirFileCount("C:\\Users\\WNDWS\\Downloads\\")) {
- Thread.sleep(2000);
- }
- Thread.sleep(5000);
- }
- public static int dirFileCount(String path) {
- File directory = new File(path);
- int filecount = directory.list().length;
- return filecount;
- }
- public static void nos() throws InterruptedException {
- AbrirSite(URL1);
- // FAZER LOG IN
- LogIn("//*[@id=\"Username\"]", "//*[@id=\"Password\"]", "/html/body/div[2]/form/button");
- ClicarBotaoXpath("/html/body/div[2]/div/div[2]/div/section[3]/a");
- // String elemento1 =
- // "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section[";
- // String elemento2 = "]/div/div/div/div[3]/div/button";
- // String elem3 =
- // "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section[";
- // String elem4 = "]/div/div/div/div[3]/div/div/ul/li[2]/a";
- // MUDAR A TAB
- ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
- driver.switchTo().window(tabs2.get(1));
- String servicosURL = driver.getCurrentUrl(); // GERIR SERVIÇOS
- List<String> contas = readFile("./src/nos.txt");
- for (int i = 0; i < contas.size(); i++) {
- String conta = contas.get(i);
- PesquisarPorXpath("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/input",
- conta);
- Esperar("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/span/i");
- Thread.sleep(2000);
- ClicarBotaoXpath("/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/header/div/div/div/span/i");
- WaitForJavascript();
- WaitForAjax();
- 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");
- ClicarBotaoXpath(
- "/html/body/div[1]/main/div/div[2]/div[2]/div/section/div/div/div[1]/section/div/div/div/div[3]/div/button");
- ClicarBotaoXpath(
- "/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");
- WaitForJavascript();
- WaitForAjax();
- Boolean faturas = Present(
- "/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");
- if (faturas) {
- ClicarBotaoXpath(
- "/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");
- ClicarBotaoXpath(
- "/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");
- }
- AbrirSite(servicosURL);
- }
- /*
- * Boolean contas = false; int i = 1; String valor; valor = Integer.toString(i);
- * do {
- *
- * ClicarBotaoXpath(elemento1 + valor + elemento2);
- *
- * ClicarBotaoXpath(elem3 + valor + elem4);
- *
- * WaitForAjax();
- *
- * contas = Present(
- * "/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"
- * );
- *
- * if (contas) { ClicarBotaoXpath(
- * "/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"
- * ); ClicarBotaoXpath(
- * "/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"
- * ); } AbrirSite(servicosURL); i++; valor = Integer.toString(i); contas =
- * false;
- *
- * } while (!driver.findElements(By.xpath(elemento1 + valor +
- * elemento2)).isEmpty());
- */
- // LOG OUT
- ClicarBotaoXpath("//*[@id=\"HeaderPlaceHolder\"]/div[2]/div/div/div/div[4]/div/div/button/span[2]");
- ClicarBotaoXpath("//*[@id=\"HeaderPlaceHolder\"]/div[2]/div/div/div/div[4]/div/div/div/a");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement