Abhishek Dalakoti Abhishek Dalakoti - 1 month ago 8
Java Question

finding the tag name which was asked for max number of times in the current year and also print the count

enter image description here public void test() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().Timeouts().implicitlyWait(10, TimUnit.SECONDS);
driver.get("http://stackoverflow.com");
driver.manage().window().maximize();
driver.findElement(By.id("nav-tags")).click();
WebElement element = driver.findElement(By.name("tagfilter"));
element.sendKeys("qa");
element.submit();
}

I have wrote the above code but what would be the next?

Answer
I would assume that the most popular tag is the first one shown in the tags page.

Based on above assumption, you can try out the following code:

System.setProperty("webdriver.chrome.driver", "G:\\naveen\\mywork\\testing\\automation\\tools\\webdrivers\\chromedriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://stackoverflow.com");
    driver.manage().window().maximize();
    driver.findElement(By.id("nav-tags")).click();
    WebElement element = driver.findElement(By.name("tagfilter"));
    element.sendKeys("qa");
    Thread.sleep(2000);
    element.sendKeys(Keys.ENTER);
    Thread.sleep(3000);
    WebElement popularTag = driver.findElement(By.xpath("//table[@id='tags-browser']/tbody/tr[1]/td[1]/a"));
    System.out.println("most popular tag: " + popularTag.getText());
    WebElement populatTagCount = driver.findElement(By.xpath("//table[@id='tags-browser']/tbody/tr[1]/td[1]/span/span[2]"));
    System.out.println("popular tag count " + populatTagCount.getText());
    WebElement tagCountAnual = driver.findElement(By.xpath("//table[@id='tags-browser']/tbody/tr[1]/td[1]/div[2]/div[1]/a[2]"));
    System.out.println("anaual count : " + tagCountAnual.getText());
    int anaualCount = Integer.parseInt(tagCountAnual.getText().split(" ")[0]);
    System.out.println("count " + anaualCount);
    driver.close();

Following is the output that i got:

most popular tag: qa
popular tag count 712
anaual count : 133 this year
count 133

you can cross check in the image:

enter image description here