We can capture the screenshot of a specific element rather than the entire page using Selenium webdriver. There may be requirements in the project where we have to capture the screenshot of a particular webelement.First of all we shall capture the screenshot of the entire page and then crop it according to the dimension and location of the element. We shall take the help of the TakeScreenShot interface and use its getScreenshotAs method.SyntaxFile i = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);Next store the image in a file [for example, .jpeg, .png].FileUtils.copyFile(i, new File(""))ExampleCode Implementationimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.io.File; ... Read More
We can select the parent element of a known element with Selenium webdriver. First of all we have to identify the known element with the help of any of the locators like id, classname and so on. Then we have to identify its parent element with findElement(By.xpath()) method.We can identify the parent element from its child by localizing it with the child and then passing ( ./..) as a parameter to the findElement(By.xpath()). Let us identify the parent element with tagname ul from the child element with the tagname li in below html code −Also we can identify the parent element ... Read More
We can wait until the page is loaded with Selenium webdriver. There is a synchronization concept in Selenium which describes implicit and explicit wait. To wait until the page is loaded we shall use the explicit wait concept.The explicit wait is designed such that it is dependent on the expected condition for a particular behavior of an element. For waiting until the page is loaded we shall use the expected condition presence_of_element_loaded for a particular element. Once the wait time is elapsed, the timeout error shall be thrown.To implement explicit wait conditions, we have to take help of the WebDriverWait ... Read More
We can run Javascript in Selenium webdriver with Python. The Document Object Model communicates with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking the help of the execute_script method. The commands to be executed are passed as arguments to the method.Some operations like scrolling down in a page cannot be performed by Selenium methods directly. This is achieved with the help of Javascript Executor. The window.scrollTo method is used to perform scrolling operation. The pixels to be scrolled horizontally along x axis and pixels to be scrolled vertically along y axis ... Read More
We can handle windows file upload with Selenium webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].This is only applied to an element having a type attribute set to file as value along with the element tagname as input. The below html code shows the element with type = file value set.ExampleCode Implementationimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class WndsFileUpl{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver ... Read More
We can perform scrolling to an element using Selenium webdriver. This can be achieved in multiple ways. Selenium cannot handle scrolling directly. It takes the help of the Javascript Executor and Actions class to do scrolling action.First of all we have to identify the element up to which we have to scroll to with the help of any of the locators like class, id, name and so on. Next we shall take the help of the Javascript Executor to run the Javascript commands. The method executeScript is used to execute Javascript commands in Selenium. We have to use the scrollIntoView method ... Read More
We can set browser width and height in Selenium webdriver. There are multiple methods available to achieve this. Whenever an application is launched, it opens in its default browser size.We can resize the browser with the help of the Dimension class in Java. We create an object of the Dimension class and pass the desired width and height of the browser as parameters to that class. Finally we pass the object of the Dimension class as an argument to the setSize method.SyntaxDimension dem = new Dimension(750, 450); driver.manage().window().setSize(dem);We can also set the browser width and height with the help of Chrome ... Read More
We can get the return value of Javascript code with Selenium webdriver. Selenium can run Javascript commands with the help of executeScript method. The Javascript command to be executed is passed as an argument to the method.We shall be returning the value from the Javascript code with the help of the keyword return. Also we have to add the statement import org.openqa.selenium.JavascriptExecutor to work with Javascript.SyntaxJavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("return document.getElementsByName('txtSearchText')[0].value")Let us obtain the value entered in the edit box. The output should be Selenium.ExampleCode Implementationimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ... Read More
We can verify an xpath expression in Chrome Developer tool or with Firefox Firebug. We can open the Developer tool in Chrome by pressing F12, then the Console tab is to be selected. We can validate the xpath with the $x("") expression.On clicking Enter after entering the expression, an array of matching elements will be displayed. On hovering over the returned result, the actual element gets highlighted on the page. If there are no matching elements, an empty array shall be returned.We also verify the xpath expression from the Firefox’s Firebug. First of all, we have to install the extensions ... Read More
We can open Chrome browser in Selenium webdriver. We can launch Chrome by instantiating an object of the ChromeDriver class with the help of the below statement.WebDriver driver = new ChromeDriver();Next we have to download the chromedriver and configure it to our project by following the below step by step processes −Navigate to the link − https://www.selenium.dev/downloads/ and below the Browser, there is a Chrome section available. Click on the documentation link just below that.As per version of the Chrome browser in the system, we have to select the download link. The next page shall be navigated where links to ... Read More