Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ad;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class ff {
- static WebDriver driver = new FirefoxDriver();
- public static void main(String[] args) throws InterruptedException {
- linkOnliner();
- linkCatalog();
- linkMobileCatalog();
- searchAction();
- chooseDevice();
- comparePrices();
- Integer[] prices = takePrices();
- MinMaxResult result = foundMinMax(prices);
- System.out.println("Минимальная цена: " + result.getMin() + ", количество предложений = " + result.getCountMin());
- System.out.println("Максимальная цена: " + result.getMax() + ", количество предложений = " + result.getCountMax());
- linkOnliner();
- }
- private static void linkOnliner(){
- // And now use this to visit Onliner
- driver.get("http://www.onliner.by");
- }
- private static void linkCatalog() {
- //Catalog's locator
- WebElement catalogElement = driver.findElement(By.cssSelector("span.b-main-navigation__text"));
- catalogElement.click();
- }
- private static void linkMobileCatalog() {
- // Mobile's device locator
- WebElement mobileElement = driver.findElement(By.cssSelector("a.catalog-bar__link.catalog-bar__link_strong"));
- mobileElement.click();
- }
- private static void searchAction(){
- // //QuickSearch;s locator
- WebElement qSearch = driver.findElement(By.name("query"));
- qSearch.sendKeys("Samsung Galaxy A3 (2016) Black [A310F]");
- qSearch.submit();
- }
- private static void chooseDevice(){
- //ATTENTION!! УВАГА!! AHTUNG - обход системы
- //не получается найти ссылку - какая-то муть с вэйтерами, надо спрашивать
- driver.get("http://catalog.onliner.by/mobile/samsung/a310fb");
- }
- private static void comparePrices(){
- //CompareButton
- WebElement compareWith = driver.findElement(By.linkText("Сравнить предложения"));
- compareWith.click();
- }
- private static Integer[] takePrices(){
- //Take Prices
- List<WebElement> priceList = driver.findElements(By.className("js-currency-primary"));
- Integer[] prices = new Integer[priceList.size()];
- //вытянул значения цен в массив
- for (int i = 0; i < priceList.size(); i++) {
- prices[i] = Integer.parseInt(priceList.get(i).getText().replaceAll("[^\\d]", "")); //all except 0-9
- }
- return prices;
- }
- private static MinMaxResult foundMinMax (Integer[] prices){
- int min = prices[0];
- int max = prices[0];
- int countMin = 0;
- int countMax = 0;
- for (int j = 0; j < prices.length; j++) {
- if (prices[j] < min){
- min = prices[j];
- countMin = 1;
- }
- else if (prices[j] == min)
- countMin++;
- if (prices[j] > max){
- max = prices[j];
- countMax = 1;
- }
- else if (prices[j] == max)
- countMax++;
- }
- MinMaxResult result = new MinMaxResult();
- result.setCountMax(countMax);
- result.setCountMin(countMin);
- result.setMax(max);
- result.setMin(min);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement