Selenium Articles

Page 20 of 37

How to check if dom has a class using WebDriver (Selenium 2)?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 486 Views

We can check if DOM has a class using Selenium webdriver. We can use the findElements method to obtain the list of elements having a particular class. Then pass By.className or By.xpath or By.cssSelector as a parameter to the method.The class name we want to search is passed as a parameter to that method. Let us investigate the below html code for an ul element having class value as toc chapters. Then obtain its sub-elements.Exampleimport 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 ClassAttrb{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",     ...

Read More

WebDriver executeAsyncScript vs executeScript in Selenium

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 3K+ Views

There are differences between executeAsyncScript and executeScript methods. For an executeScript method, JavaScript Executor runs the JavaScript with the reference to the present selected window or frame. The script within the method shall run as the body of the unnamed function.Inside the script, the documents are used to point to the present document. Also, the local variables will not be present as the script has completed execution. However, the global variables shall be present.If the script consists of a return statement, then the below rules are followed −A webelement is returned for an HTML element.A double is returned for a ...

Read More

How do you make Selenium 2.0 wait for the page to load?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 620 Views

We can make Selenium wait for the page to load. We can use the synchronization concept in Selenium to wait for page loading. The implicit wait is a type of synchronization applied to elements to wait for a specified amount of time.Syntaxdriver.manage().timeouts().implicitlyWait();We can also call the JavaScript method document.readyState and wait till it yields the value complete. Selenium executes JavaScript command with the help of the executeScript method.SyntaxJavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("return document.readyState").toString().equals("complete");Post this step, we should check if the URL is similar to the one we are looking for.ExampleCode Implementation with implicit wait.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import ...

Read More

How do I verify that an element does not exist in Selenium 2?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 7K+ Views

We can verify if an element does not exist in Selenium webdriver. To achieve this, we shall use the method getPageSource which gets the entire page source. So we can obtain a complete page source and check if the text of the element exists.We also use the findElements method and utilize any locators like xpath, CSS, and so on to identify the matching elements. The findElements gives an elements list.We shall count the number of elements returned by the list with the help of the size method. If the value of size is greater than 0, then the element exists ...

Read More

How to integrate Sikuli scripts into Selenium?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 2K+ Views

We can integrate Sikuli scripts into Selenium webdriver. Sikuli is an automation tool which is open-source. It has the feature to capture the images on the elements as well as perform operations on them.Some of the advantages of Sikuli are −Desktop or Windows applications can be automated.Can be used for flash testing.Can be used on platforms like mobile, Mac, and Linux.It is based on image recognition technique.Can be easily integrated with Selenium.To integrate Sikuli with Selenium, follow the below steps − Navigate to the link − https://launchpad.net/sikuli/+download.Click on the jar to download it (which can be used for Java environments) ...

Read More

How to Resolve Stale Element Reference Exception in Selenium WebDriver?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 7K+ Views

We can resolve StaleElementReferenceException in Selenium webdriver. The term stale means something which is not fresh and decayed. Thus a stale element points to an element which is not present any more.There may be a case, when an element was in DOM initially but after modifications in Document Object Model (DOM), the element becomes stale and the StaleElementReferenceException is thrown if we make an attempt to access this element.This exception is caused whenever an element is not present in the DOM, or deleted. We can handle this exception by the following ways −Refreshing the page and verifying again.Implement retry method.ExampleCode ...

Read More

How to convert commands recorded in Selenium IDE to Java?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 2K+ Views

We can convert commands recorded in Selenium IDE to Java. To convert commands first navigate to File menu, then select the option Export Test case As.After clicking it, all the possible options for conversion get displayed. Choose the option Java/ Junit 4/ WebDriver.Finally, we have to save the file with the .java extension. Then we can open this file using a text Editor or IDE.We can also convert commands recorded in Selenium IDE to Java by navigating to the Options menu. As the Selenium IDE Options pop−up comes up, select the checkbox Enable experimental features. Then click on OK.Next, select ...

Read More

Can Selenium interact with an existing browser session?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 5K+ Views

We can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.This is generally used for debugging purposes when we have a large number of steps in a test and we do not want to repeat the same steps. First of all we shall launch the browser and enter some text in the below edit box.Exampleimport org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.Capabilities; import org.openqa.selenium.By; import java.util.Map; import java.util.concurrent.TimeUnit; public class ConnectExistingSession{    public static void main(String[] args)    throws ...

Read More

How do I set the Selenium webdriver get timeout?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 5K+ Views

We can set the Selenium webdriver to get timeout. There are numerous methods to implement timeouts. They are listed below −setScriptTimeout.pageLoadTimeout.implicitlyWait.The setScriptTimeout is the method to set the time for the webdriver. This is usually applied for an asynchronous test to complete prior throwing an exception. The default value of timeout is 0.This method is generally used for JavaScript commands in Selenium. If we omit setting time for the script, the executeAsyncScript method can encounter failure due to the more time consumed by the JavaScript to complete execution.If the timeout time is set to negative, then the JavaScript can execute ...

Read More

How to execute a Javascript function in Python with Selenium?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 01-Feb-2021 3K+ Views

We can execute a JavaScript function in Python with Selenium webdriver. DOM interacts with the elements via JavaScript. Selenium is capable of executing JavaScript commands with the execute_script method.Few actions like web scrolling cannot be done by Selenium directly. For this, we shall use the JavaScript Executor. We shall take the help of the JavaScript command window.scrollTo and pass it to the execute_script method. To scroll to the bottom of the page, we have to pass 0 and document.body.scrollHeight as parameters to the window.scrollTo.Syntaxdriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")Examplefrom selenium import webdriver driver = webdriver.Firefox(executable_path="C:\geckodriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") #scroll till page bottom driver.execute_script("window.scrollTo(0, document.body.scrollHeight);)We can ...

Read More
Showing 191–200 of 362 articles
« Prev 1 18 19 20 21 22 37 Next »
Advertisements