kimleekim / Spring-Pllabel

Our Project Pllabel! : Spring framework-mvc: 5.1.8, Gradle, Mysql, Tomcat9, jdk1.8
0 stars 1 forks source link

장소 태그 url 따기 #62

Open juhyeonme opened 5 years ago

juhyeonme commented 5 years ago

@Override public String setUpWebDriverForLocation(String tempUrl, String keyword) throws Exception { List searchList; WebElement search = null; String url = "";

    this.driver = driverContext.setupChromeDriver();
    this.webDriverWait = new WebDriverWait(this.driver, 40);
    driver.manage().window().maximize();
    driver.get(tempUrl);

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input");
        webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);
        webDriverWait.until(ExpectedConditions.textToBePresentInElementValue(this.findWebElement, keyword));
    } catch(TimeoutException e) {
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);

        webDriverWait.until(new ExpectedCondition<Boolean>() {

                    public Boolean apply(WebDriver driver) {
                        WebElement webElement
                                = driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input"));
                        String check = webElement.getAttribute("value");

                        if(check.equals(keyword))
                            return true;
                        else
                            return false;
                    }

                            });

                System.out.println("검색함 : " + driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input")).getAttribute("value"));
    }

    webDriverWait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {
            By by = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[3]");
            String check = driver.findElement(by).getAttribute("class");

            if(check.equals("aIYm8 coreSpriteSearchClear"))
                return true;
            else
                return false;
        }
    });

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div");
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));

    } catch(NoSuchElementException
            | TimeoutException e) {

        try {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        } catch(NoSuchElementException e2) {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        }

    }

    webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));

    searchList = driver.findElements(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div"));

    try {
        search = searchList.get(0).findElement(By.xpath(".//a"));
    } catch(NoSuchElementException e) {
        int i = 0;

        for (WebElement block : searchList) {
            if (block.findElement(By.xpath(".//a[" + i + "]/div/div[1]")).getAttribute("class").equals("nebtz coreSpriteLocation")
                    && block.findElement(By.xpath(".//a[" + i + "]/div/div[2]/div/span")).getText().equals(keyword)) {

                search = block.findElement(By.xpath(".//a[" + i + "]"));
                break;
            }
            i++;
        }
    }

        if (search != null) {
            url = search.getAttribute("href");
        }
        else
            return "";

    this.driver.quit();
    return url;
}
juhyeonme commented 5 years ago
private static String tempUrl = "https://www.instagram.com/explore/tags/temp";
url = this.instaCrawlImpl.setUpWebDriverForLocation(tempUrl, hotplace);
juhyeonme commented 5 years ago

수정 :

@Override public String setUpWebDriverForLocation(String tempUrl, String keyword) throws Exception { WebElement getSearch; WebElement search = null; String url = "";

    this.driver = driverContext.setupChromeDriver();
    this.webDriverWait = new WebDriverWait(this.driver, 40);
    driver.manage().window().maximize();
    driver.get(tempUrl);

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input");
        webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);
        webDriverWait.until(ExpectedConditions.textToBePresentInElementValue(this.findWebElement, keyword));
    } catch(TimeoutException e) {
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);

        webDriverWait.until(new ExpectedCondition<Boolean>() {

                    public Boolean apply(WebDriver driver) {
                        WebElement webElement
                                = driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input"));
                        String check = webElement.getAttribute("value");

                        if(check.equals(keyword))
                            return true;
                        else
                            return false;
                    }

                            });

                System.out.println("검색함 : " + driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input")).getAttribute("value"));
    }

    webDriverWait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {
            By by = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[3]");
            String check = driver.findElement(by).getAttribute("class");

            if(check.equals("aIYm8 coreSpriteSearchClear"))
                return true;
            else
                return false;
        }
    });

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div");
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));

    } catch(NoSuchElementException
            | TimeoutException e) {

        try {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        } catch(NoSuchElementException e2) {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        }

    }

    webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));

    getSearch = driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div"));

    try {
        search = getSearch.findElement(By.xpath(".//a"));
    } catch(NoSuchElementException e) {
        int i = 1;

        while(true) {
            if (getSearch.findElement(By.xpath(".//a[" + i + "]/div/div[1]")).getAttribute("class").equals("nebtz coreSpriteLocation")
                    && getSearch.findElement(By.xpath(".//a[" + i + "]/div/div[2]/div/span")).getText().equals(keyword)) {

                search = getSearch.findElement(By.xpath(".//a[" + i + "]"));
                break;
            }
            i++;
        }
    }

        if (search != null) {
            url = search.getAttribute("href");
        }
        else
            return "";

    this.driver.quit();
    return url;
}
juhyeonme commented 5 years ago

로그인 삽입 :

private void checkLogin(String search) { String url = this.driver.getCurrentUrl(); By locator; if(url.contains("accounts/login/?next=/")) { locator = By.cssSelector("#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(2) > div > label > input"); webDriverWait.until(ExpectedConditions.elementToBeClickable(locator));

        driver.findElement(locator).sendKeys("yunmyeong123");
        locator = By.cssSelector("#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(3) > div > label > input");
        driver.findElement(locator).sendKeys("project123");

        driver.findElement(locator).sendKeys(Keys.ENTER);
        driver.findElement(locator).submit();
        try {
            webDriverWait.until(ExpectedConditions.not(ExpectedConditions.urlContains("accounts/login/?next=/")));
        }
        catch(TimeoutException e) {
            driver.findElement(locator).sendKeys(Keys.ENTER);
            driver.findElement(locator).submit();
            webDriverWait.until(ExpectedConditions.not(ExpectedConditions.urlContains("accounts/login/?next=/")));
        }
    }
}

@Override
public WebDriver setUpWebDriver (String search, String station) throws Exception {
    //초기 드라이버 셋팅
    if (search.contains(station)) { isFoodpost = 1; }
    else { isFoodpost = 2; }

    this.driver = driverContext.setupChromeDriver();
    this.webDriverWait = new WebDriverWait(this.driver, 40);
    driver.manage().window().maximize();
    this.skipPopularposts = 9;
    driver.get(search);

    webDriverWait.until(ExpectedConditions.urlContains("explore/locations/"));
    checkLogin(search);

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/main/header/div[2]/div/div[2]/span/span");
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(findWebElement));
        this.element = driver.findElement(findWebElement);
        totalPostCount = Integer.parseInt(element.getText().replaceAll("\\,", ""));
        System.out.println("total: " + totalPostCount);
    } catch (TimeoutException e) {
        logger.info("FAIL to get Total-posts-Count.");
        refreshPage(driver);
    }

    return driver;
}

@Override
public String setUpWebDriverForLocation(String tempUrl, String keyword) throws Exception {
    List<WebElement> getSearch;
    WebElement search = null;
    String url = "";

    this.driver = driverContext.setupChromeDriver();
    this.webDriverWait = new WebDriverWait(this.driver, 40);
    driver.manage().window().maximize();
    driver.get(tempUrl);

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input");
        webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);
        webDriverWait.until(ExpectedConditions.textToBePresentInElementValue(this.findWebElement, keyword));
    } catch(TimeoutException e) {
        this.element = driver.findElement(this.findWebElement);

        this.element.clear();
        this.element.sendKeys(keyword);

        webDriverWait.until(new ExpectedCondition<Boolean>() {

                    public Boolean apply(WebDriver driver) {
                        WebElement webElement
                                = driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/input"));
                        String check = webElement.getAttribute("value");

                        if(check.equals(keyword))
                            return true;
                        else
                            return false;
                    }

                            });

    }

    webDriverWait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {
            By by = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[3]");
            String check = driver.findElement(by).getAttribute("class");

            if(check.equals("aIYm8 coreSpriteSearchClear"))
                return true;
            else
                return false;
        }
    });

    try {
        this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div");
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));

    } catch(NoSuchElementException
            | TimeoutException e) {

        try {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        } catch(NoSuchElementException e2) {
            this.findWebElement = By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a");
            webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.findWebElement));
        }

    }

    webDriverWait.until(ExpectedConditions.elementToBeClickable(this.findWebElement));

    getSearch = driver.findElements(By.xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a"));

    for(WebElement block : getSearch) {
        if(block.findElement(By.xpath(".//div/div[1]")).getAttribute("class").equals("nebtz coreSpriteLocation")
                && block.findElement(By.xpath(".//div/div[2]/div/span")).getText().equals(keyword)) {

            search = block;
            break;
        }
    }

    if (search != null) {
        url = search.getAttribute("href");
    }
    else
        return "";

    this.driver.quit();
    return url;
}