0

I want to iteratively click each item in a structure of links and open each link in a new tab. I also want to get and print the title of each tab.

example site footer links

    package basics;
    
    import java.util.Iterator;
    import java.util.Set;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class Naukri {
        public static void main(String[] args) throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", "C://chromedriver_win32//chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.naukri.com/top-skill-jobs");
            driver.manage().window().maximize();
            WebElement s = driver.findElement(By.xpath("//*[@class = 'footer anchorList']//*[@class='wrap']"));
            System.out.println(s.findElements(By.tagName("a")).size());

            // Attempt to open each link in a new tab
            for (int i=0;i<s.findElements(By.tagName("a")).size(); i++) {
                String clickonlinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
                s.findElements(By.tagName("a")).get(i).sendKeys(clickonlinkTab);
                Thread.sleep(3000L);
            }
    
            // Attempt to print all the open tab names.
            Set<String> ListofLinks = driver.getWindowHandles();
            Iterator<String> it = ListofLinks.iterator();
            while (it.hasNext()) {
                driver.switchTo().window(it.next());
                System.out.println(driver.getTitle());
    
            } 
            driver.close();
        }
    }
8
  • Could you please explain more on this? – SQA_LEARN Feb 6 '19 at 19:19
  • I want to open all 69 links in new tabs and going forward want to print all those 69 links title links on my console – Ashutosh Singh Feb 6 '19 at 19:24
  • Explain more on issue side also. It helps others to understand where it went wrong. – SQA_LEARN Feb 6 '19 at 19:27
  • 1
    @AshutoshSingh. That's quite overkill. It's like using cannon to kill a fly -- the whole process will be pretty slow and resource consuming. Why do you use Selenium for that? – dzieciou Feb 6 '19 at 19:29
  • 1
    Suggestion: Be Clear with question, Clearly explain what you are trying to do. what you tried and where you got struck. also provide required samples/examples/URLs – SQA_LEARN Feb 6 '19 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.