Found 519 Articles for Selenium

How to close child browser window in Selenium WebDriver using Java?

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:40:34

5K+ Views

We can close the child browser window in 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 browser window currently active. To iterate over the window handles, the iterator method is used. We have to add import java.util.Set to accommodate Set and import java.util.List and import java.util.Iterator statements to accommodate iterator in our code.By default, the driver object can access the elements of the parent window. In order to switch its focus ... Read More

How to set Selenium Python WebDriver default timeout?

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:38:44

6K+ Views

We can set default timeout with Selenium webdriver. The method set_page_load_timeout is used to have a timeout for the page loading. The wait time in seconds is passed as parameter to the method.Syntaxdriver.set_page_load_timeout(5)A TimeoutException is thrown if the page is still not loaded after the wait time is passed.We can use the implicit wait concept in synchronization to define the default timeout time. This is a global wait time and applied to every element in the page. The method implicitly_wait is used to define implicit wait. The wait time in seconds is passed as parameter to the method.Syntaxdriver.implicitly_wait(5);A TimeoutException is ... Read More

How to use the gecko executable with Selenium?

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:37:18

217 Views

We can use gecko executable driver with Selenium webdriver. For the Mozilla version above 47, the geckodriver is used due to the presence of Marionette, which is the driver for automation in Mozilla. We can launch the Firefox by instantiating the object of FirefoxDriver class with the help of the below statement.WebDriver driver=new FirefoxDriver();Next we have to download the geckodriver and configure it to our project by following the below step by step processes −Navigate to the link − https://www.selenium.dev/downloads/ and move below the Browser text, there is a Firefox section available. Click on the Documentation link just below that.All the geckodriver ... Read More

How to select specified node within Xpath node sets by index with Selenium?

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:33:17

5K+ Views

We can select specific nodes within xpath node sets by index with Selenium webdriver. We can mention a specific node with the help of its index number enclosed in [].Let us have a look at the below html code for an element having children. The element with tagname ul has multiple child elements with tagname li. If we want to identify the second child of the parent having text as Software Quality management the xpath expression with node index shall be //ul[@class='list']li[2].The xpath expression to identify the second child of the parent element ul can also be created with the ... Read More

Accessing HTML source code using Python Selenium.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:29:21

3K+ Views

We can access HTML source code with Selenium webdriver. We can take the help of the page_source method and print the value obtained from it in the console.Syntaxsrc = driver.page_sourceWe can also access the HTML source code with the help of Javascript commands in Selenium. We shall take the help of execute_script method and pass the command return document.body.innerHTML as a parameter to the method.Syntaxh = driver.execute_script("return document.body.innerHTML;")ExampleCode Implementation.from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # access HTML source code with page_source method s = driver.page_source print(s)Code Implementation with Javascript Executor.from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) ... Read More

Reading JavaScript variables using Selenium WebDriver.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:27:47

2K+ Views

We can read Javascript variables 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. Also we have to add the statement import org.openqa.selenium.JavascriptExecutor to work with Javascript.SyntaxJavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("return document.title")Let us obtain the browser title of the below page by reading the value from Javascript variable. The output should be About Careers at Tutorials Point – Tutorialspoint.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 JavascriptReadValue{    public static void main(String[] args) {   ... Read More

How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:24:41

1K+ Views

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

Select parent element of known element in Selenium.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:22:49

9K+ Views

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

Wait until page is loaded with Selenium WebDriver for Python.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:19:23

2K+ Views

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

Running javascript in Selenium using Python.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:16:07

599 Views

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

Advertisements