Selenium Interview Questions 216u1q

  • ed by: Sujit Tupe
  • 0
  • 0
  • August 2020
  • PDF

This document was ed by and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this report form. Report 2z6p3t


Overview 5o1f4z

& View Selenium Interview Questions as PDF for free.

More details 6z3438

  • Words: 2,285
  • Pages: 12
Code to switch into and out of frame : WebElement targetFrame = driver.findElement(By.className(“demo-frame”)); driver.switchTo().frame(targetFrame); ….Do something…. driver.switchTo.defaultContent(); //Switches back to the original page

28. How to check if an element is present in the web page? To find if an element is present, we first find all the elements with the locator (xpath, cssSelector, id etc.). If any number of elements are present, they will be returned as a list. We get the size of the list. If size is equal to 0, element is not present; if greater than 0, element is present. This is especially useful if you have multiple frames and you want to locate the element but don’t know in which frame the element is present. Of course, you need to find the element’s xpath (or other locators). 29. Where and when can you use Selenium’s javascript executor? Javascript executor method is useful when you cannot get dynamic elements through normal Selenium methods. The html elements are either hidden or not accessible directly by selenium. More prominent example where you need to use Javascript executors are when

P a g e | 11

you want to retrieve Autosuggestion text box’s suggestions. Since these suggestions are displayed only when partially types input and are thus generated dynamically, it is often not possible to get the locator of the suggestion text. When you try to inspect the suggestion elements, they just often disappear. In such cases, you can use javascript executor methods to read the elements using DOM (instead of Selenium). 30. Code for Javascript executor for Autosuggestion boxes WebDriver driver=new ChromeDriver(); driver.get("https://www.ksrtc.in"); driver.findElement(By.xpath("//input[@id='fromPlaceName']")).sendKeys("BENG"); Thread.sleep(2000); driver.findElement(By.xpath("//input[@id='fromPlaceName']")).sendKeys(Keys.DOWN); System.out.println(driver.findElement(By.xpath("//input[@id='fromPlaceName']")).getText()); //Javascript DOM can extract hidden elements //because selenium cannot identify hidden elements - (Ajax implementation) //investigate the properties of object if it has any hidden text //JavascriptExecutor JavascriptExecutor js= (JavascriptExecutor)driver; String script = "return document.getElementById(\"fromPlaceName\").value;"; String text=(String) js.executeScript(script); System.out.println(text); int i =0; //BENGALURU INTERNATIONAL AIRPORT while(!text.equalsIgnoreCase("BENGALURU INTERNATIONAL AIRPORT")) { i++; driver.findElement(By.xpath("//input[@id='fromPlaceName']")).sendKeys(Keys.DOWN); text=(String) js.executeScript(script); System.out.println(text); if(I > 10) { break; } } if(I > 10) { System.out.println("Element not found"); } else { System.out.println("Element found"); }

P a g e | 12

31. How to accept insecure and SSL certificates in Selenium? We use instantiate Selenium class DesiredCapabilities and call its method acceptInsecureCerts() or setCapability() which sets properties. The DesiredCapabilities object is ed to ChromeOptions class’s merge() method. The ChromeOptions is is a parameter ed to the WebDriver constructor. Code: DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.acceptInsecureCerts(); // Use this //OR capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); ChromeOptions chrOptions= new ChromeOptions(); chrOptions.merge(capabilities); System.setProperty("webdriver.chrome.driver", "D:\\devel\\ chromedriver.exe"); WebDriver driver=new ChromeDriver(chrOptions);

32. Maximizing and minimizing windows in Selenium driver.manage(),window().maximize(); driver.manage(),window().minimize();

33. Deleting cookies To delete all cookies: driver.manage().deleteAllCookies(); To delete specific cookie, we the cookie name as parameter: driver.manage().deleteCookieNamed(“sessionKey_aas34w1”);

34. Taking screenshot in Selenium Screehshots can be captured using TakeScreenshot class: File img = ((TakeScreenshot)driver).getScreenshotAs(OutputType.FILE); //We need FileUtils class from Apache: org.apache.commons.io.FileUtils //We save the above screenshot in a file FileUtils.copyFile(img, new File(C://Documents//screenshot.png)); Note: If you try to save the file to C: (eg. C:// screenshot.png) you will get Access violation error. So create a directory under C: drive or save it in some sub directory under C:

Related Documents c2h70

August 2020 0
Selenium Java Interview Questions 5a1m2n
July 2020 2
Selenium Interview Questions 216u1q
August 2020 0
Selenium Interview Questions 216u1q
January 2021 0
Selenium Interview Questions And Answers 6d6130
October 2019 67
100 Best Selenium Job Interview Questions 2j3a1k
August 2020 0

More Documents from "Sujit Tupe" 4o6xj

Selenium Interview Questions 216u1q
August 2020 0
Powermill2017-5axis_6-1-2017.pdf 694i2e
November 2019 122
Third Party Logistics_final Report 2l1b6a
April 2020 29
Harpic Case Study Analysis 1u5e5
December 2019 58
Strategic Management Short Project For Tata Motors 6j4w6g
October 2019 66
Backgroud-of-malaysia-airlines (1).docx 3j2hn
November 2019 50