Interview Codes

 

 

 

 

 

How to find numbers of url’s in a given webpage in selenium.

 public void getUrls1() {
        int urls;
        List<WebElement> urllist;
       
       
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/resources/drivers/chromedriver.exe");
        driver= new ChromeDriver();
        driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");  //this line can be removed if you want to keep changing the url based on need
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        urls=driver.findElements(By.tagName("a")).size();   
        urllist=driver.findElements(By.tagName("a"));
        System.out.println("no of Urls in the login page are-"+urls);
       
       
        int i=1;
        for(WebElement temp:urllist) {
        System.out.println("weblink"+i+" is -"+temp.getText());
        i++;
        }

 

 How to find nearest square root of a given number.. 

public int getSquareroot(int input) {
        int i=0,sqr=0,j,k;
        

// Logic to find square root
        while(sqr<input)
            {
            sqr=i*i;
            i++;
           
            };
       
            j=i-1;     // j or k could be the nearest square root at this point
            k=i-2;
               
            // Code to the closest square root between j & k
            if((j*j)<input) {
               
                if(input-(j*j) < input-(k*k))
                return j;
                else
                return k;
               
               
            }
           
            else {
           
                if((j*j)-input < input-(k*k))
                    return j;
                    else
                    return k;
            }
       
           
        //return j;
    }


  • how do you identify unique word counts from the given String.

Example: String s = "cat dog cat tiger rat cat dog" how do get output as cat=3 times, dog=2 times, tiger=1,rat=1

public void UniqueWordCount_method1(String S) {
       
String[] wc=S.split(" ");
expect = new HashSet<String>();

for(String temp : wc){
expect.add(temp);
}

Iterator<String> itr=expect.iterator();

while(itr.hasNext()) {
noof=0;
String temp1=itr.next();
for(String temp : wc){
if(temp.equalsIgnoreCase(temp1)) {
noof++;
}
}
System.out.println("There are "+noof +" "+temp1);
}
    }

No comments:

Post a Comment