Wednesday, June 26, 2024

Handling Auto Suggestions

 

Handling Auto Suggestions

when we type something, it displays some suggestions/ options.

 

When working with auto-suggestions, the common tasks involve typing into an input field and then selecting one of the suggested options

 

  rama

  rama rao

  ramayya institute of technology

 

package AlertFrameWindowsBasics;

 

import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Autosuggestions {

 

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

                        driver.get("https://www.google.com/");

                        //*******************************************

                        // Enter 'rama' in search box

                        driver.findElement(By.name("q")).sendKeys("rama");

 

                        Thread.sleep(5000);

 

                        // get all suggestions 

                        //div[@class='erkvQe']//ul[@role='listbox']/li  or (//ul[@role='listbox'])[1]/li

                        // or //div[@class='erkvQe']//ul[@role='listbox']/li/div//div[@class='lnnVSe']

                       

                        // //ul[@jsname='bw4e9b']/li

                        //ul[@jsname='bw4e9b']/li//div[@class='wM6W7d']/span

//                     (//ul[@jsname='bw4e9b']/li//div[@class='mus_il'])[1]

                       

                                    List<WebElement>     autoSuggestions = driver.findElements(By.xpath("//ul[@jsname='bw4e9b']/li//div[@class='wM6W7d']/span|(//ul[@jsname='bw4e9b']/li//div[@class='mus_il'])[1]"));

                                    int autoSuggestionsCnt = autoSuggestions.size();

                                    System.out.println("autoSuggestionsCnt="+autoSuggestionsCnt);

                                   

                        //1  for loop with index no

                                    for(int i=0;i<=autoSuggestionsCnt-1;i++)

                                    {

                                                String  suggestionNames= autoSuggestions.get(i).getText();

                                                System.out.println("suggestionNames="+suggestionNames);

                                    }

                        //                     suggestionNames=ramanna s lamani

                        //                     suggestionNames=ramalinga reddy

                        //                     suggestionNames=ramamurthy nagar

                        //                     suggestionNames=ramabanam

                        //                     suggestionNames=ramaiah institute of technology

                        //                     suggestionNames=ramachari serial

                        //                     suggestionNames=ramayan

                        //                     suggestionNames=ramanagara

                        //                     suggestionNames=ramaiya vastavaiya

                        //                     suggestionNames=ramadan 2024

 

 

                        // HW 2. for each loop

 

                        // Hw 3 iterator()

 

                        //  Hw 4 use listIterator()

 

 

            }

 

}

 

 

 

Capturing Screenshots in Selenium WebDriver:

 

·  Capture Screenshot: The TakesScreenshot interface is used to capture the screenshot. The getScreenshotAs method captures the screenshot and stores it in a File object.

·  Save Screenshot: The screenshot is saved to a specified location using FileHandler.copy.

 

package AlertFrameWindowsBasics;

 

import java.io.File;

import java.io.IOException;

 

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class TakeScreenShotOfPage {

 

            public static void main(String[] args) throws IOException {

                        // class + interface ==  some predefined methods

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

//                     driver.get("https://www.google.com/");

                        driver.get("https://mvnrepository.com/artifact/commons-io/commons-io/2.16.0");

                        //**************************************

 

                        //                     TakesScreenshot(I)  - predefined Interface in Selenium

                        //   getScreenshotAs() - used to take screenshot of web page

                        // OutputType --  interface   

                        File srcFile =    ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE) ;

 

                        System.out.println("src file="+srcFile);

                        // src file=C:\Users\Lenovo\AppData\Local\Temp\screenshot12776774740905017780.png

 

                        //         Fileutils -->    commons-io .jar file

                        // Download jar file from maven repository  and add to current project

                        // https://mvnrepository.com/

                        //

                        // add "commons -io .jar" file to project =  similar to adding selenium-java .jar file to project

 

                        // copy file into destFile

                        File destFile = new File("C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SeleniumMay52024\\src\\AlertFrameWindowsBasics\\screenshot1.png");

                        FileUtils.copyFile(srcFile, destFile);

 

                        System.out.println("ends ");

 

 

            }

 

}

           

 

 

*** FAQ / IQ  Write a code to take screen shot of the page ?

 

 

How to take screenshot manually ?

press 'prnt Sc ' button in keyboard  and Paste in word document-  -- full screens shot  with task manager and date and time

           (Or)

Alt + Prt Scr  --  and paste in word document  -->     no date and time

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...