Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package selenium;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- //comment the above line and uncomment below line to use Chrome
- //import org.openqa.selenium.chrome.ChromeDriver;
- public class Selenium {
- private static final long TIME_SLEEP = 350;
- public static void main(String[] args) {
- runProgram();
- }
- public static void runProgram() {
- System.setProperty("webdriver.gecko.driver",
- "/Users/fernandorobertogonzaleztapia/downloads/gecko/geckodriver");
- WebDriver driver = new FirefoxDriver();
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- //comment the above 2 lines and uncomment below 2 lines to use Chrome
- //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
- //WebDriver driver = new ChromeDriver();
- String baseUrl = "https://login.yahoo.com/config/login?.src=fpctx&.intl=e1&.lang=es-US&.done=https%3A%2F%2Fespanol.yahoo.com";
- // launch Fire fox and direct it to the Base URL
- driver.get(baseUrl);
- driver.findElement(By.id("login-username")).sendKeys("reinhardsama");
- driver.findElement(By.id("login-signin")).click();
- driver.findElement(By.id("login-passwd")).sendKeys("R31nh4rd");
- driver.findElement(By.id("login-signin")).click();
- boolean crashed = false;
- while(!crashed) {
- try {
- restoreCat(driver);
- changeCat(driver);
- } catch(Exception e) {
- e.printStackTrace();
- crashed = true;
- }
- }
- System.out.println("The program stopped");
- driver.close();
- }
- private static void changeCat(WebDriver driver) {
- for (String url : getUrlsForCatChange()) {
- changeCat(driver, url);
- }
- }
- private static void restoreCat(WebDriver driver) {
- for (String url : getUrlsForCatRestore()) {
- restoreCat(driver, url);
- }
- }
- private static List<String> getUrlsForCatRestore() {
- List<String> urls = new ArrayList<>();
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522174112AAIhzS9");
- return urls;
- }
- private static List<String> getUrlsForCatChange() {
- Set<String> urls = new HashSet<>();
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120045AAJM4F7");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522183849AAikTTb");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182217AAdKWJC");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182144AAkF6Ln");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182054AAB9qzL");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522181826AA091uo");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522144820AACeHiJ");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522135959AAsqAVJ");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522135644AALO8VT");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522134439AAXNo9e");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522134255AAHmMPb");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522071931AAap2l8");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522071756AAh913n");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522064341AA8lEWa");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115352AA1uT8S");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115154AAdYkST");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115034AAqGEzW");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190515130614AALP8it");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190515123848AAzGrfb");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190508111138AAdiGow");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190520142317AAL3Hg3");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120536AAFFTMh");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120439AAmQ4Ei");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120300AAZIsgn");
- urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120151AAWmGZw");
- return new ArrayList<String>(urls);
- }
- private static void restoreCat(WebDriver driver, String url) {
- driver.get(url);
- driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.xpath("//*[contains(text(), 'Modificar categoría')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("parentCategory-0")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("childCategory-396545444")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("childCategory-396545450")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("ya-cat-submit")).click();
- sleep(TIME_SLEEP);
- }
- private static void changeCat(WebDriver driver, String url) {
- driver.get(url);
- driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.xpath("//*[contains(text(), 'Modificar categoría')]")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("parentCategory-0")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("childCategory-396545443")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("childCategory-396546020")).click();
- sleep(TIME_SLEEP);
- driver.findElement(By.id("ya-cat-submit")).click();
- sleep(TIME_SLEEP);
- }
- private static void sleep(long time) {
- try {
- Thread.sleep(time);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement