We can close a specific window with Selenium webdriver. The getWindowHandles and getWindowHandle methods can be used to handle child windows. The getWindowHandles method is used to store all the opened window handles in the Set data structure.The getWindowHandle method is used to store the window handle of the browser window in focus. We have to add import java.util.Set and import java.util.List statements to accommodate Set data structure in our code.By default, the driver object can only access the elements of the parent window. In order to switch its focus from the parent to the child window, we shall take ... Read More
We can select elements inside an iframe with xpath in Selenium webdriver. A frame is defined with , or tag in html code. A frame is used to embed an HTML document within another HTML document. Let us see the html code of a frame.Selenium by default has access to the parent browser driver. In order to access inside a frame, the driver focus has to shift from the main browser window to the frame. There are more than one methods to shift focus to frames −switchTo().frame(id) - The id or name of frame is passed as an ... Read More
We can set value to input webelement using Selenium webdriver. We can take the help of the sendKeys method to enter text to the input field. The value to be entered is passed as an argument to the method.Syntaxdriver.findElement(By.id("txtSearchText")).sendKeys("Selenium");We can also perform web operations like entering text to the edit box with Javascript Executor in Selenium. We shall use the executeScript method and pass argument index.value='' and webelement as arguments to the method.SyntaxWebElement i = driver.findElement(By.id("id")); JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("arguments[0].value='Selenium';", i);ExampleCode Implementationimport org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import java.util.concurrent.TimeUnit; public class SetValue{ public static void main(String[] ... Read More
We can set the style display of an html element with Selenium webdriver. The DOM interacts with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking the help of the executeScript method. The commands to be executed are passed as arguments to the method.Some operations like setting the style display be performed by Javascript Executor. The getElementById method can be used to locate the element. Then we have to apply the style.display method on the webelement and set the display type.Syntaxexecutor.executeScript ("document.getElementById('gsc-i-id1').style.display='block';");ExampleCode Implementation.import 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; ... Read More
We can get the html code of a webelement with the help of Selenium webdriver. We can obtain the innerHTML attribute to get the HTML content of the web element.The innerHTML is an attribute of a webelement which is equal to the content that is present between the starting and ending tag. The getAttribute method is used for this and innerHTML is passed as an argument to the method.SyntaxString s = element.getAttribute('innerHTML');Let us see the below html code of an element. The innerHTML of the element shall be < You are browsing the best resource for Online Education.ExampleCode Implementationimport org.openqa.selenium.WebDriver; ... Read More
We have StaleElementReferenceException in Selenium webdriver. As the name suggests, the word stale refers to something which is not new and perished. There may be a scenario in which an element which was present in DOM previously is now no longer available due to modification in DOM.In such a condition, if we try to access that element then StaleElementReferenceException is thrown. This type of exception is encountered due to the below reasons −The element is not present in the DOM any more.The element has been removed totally.There are certain ways we can prevent a StaleElementReferenceException as described below −We can ... Read More
We can handle dropdown with Selenium webdriver. The static dropdown in Selenium is handled with the Select class and the dropdown should be identified in the html code with the tag.Let us see the html code of a static dropdown.We have to add the import org.openqa.selenium.support.ui.Select statement in the code to work with the methods available in the Select class. The methods to select an option from the dropdown are listed below −selectByValue(val) – The option is selected whose value attribute matches with the argument passed to the method. This method can be used only if the dropdown option has ... Read More
We can work with iframe in Selenium webdriver. A frame is defined with , or tag in html code. A frame is used to embed an HTML document within another HTML document.Selenium by default has access to the parent browser driver. In order to access a frame element, the driver focus has to shift from the main browser window to the frame. There are more than one methods to shift to frames −switchTo().frame(id) - The id or name of frame is passed as an argument.Syntax − driver.switchTo().frame("id"), switching to the frame with id.switchTo().frame(m) - The index of frame ... Read More
We can locate elements by partial id match with Selenium webdriver. This can be achieved while we identify elements with the help of xpath or css locator. With the css and xpath expression, we use the regular expression to match id partially.Let us have a look at the id of an element in its html code. The id attribute value is gsc-i-id1.With the css expression, we can use the * and perform a partial match with the id.The css value shall be input[id*='id']. This means the subtext id is present in the actual text gsc-i-id1. We can also use the ... Read More
We can click a link with the webdriver click and Javascript click. For the Selenium webdriver click of a link we can use link text and partial link text locator. We can use the methods driver.findElement(By.linkText()) and driver.findElement(By.partialLinkText()) to click.The links in an html code are enclosed in an anchor tag. The link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.linkText()) method. The partial matching link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.partialLinkText()) method. Finally to click on the link the click method is used.Let us see the html ... Read More