Set Chrome's Language Using Selenium ChromeDriver

Debomita Bhattacharjee
Updated on 02-Feb-2021 11:38:58

6K+ Views

We can set Chrome browser language using Selenium webdriver. Some applications are designed such that it can be redesigned to multiple languages without any modifications. This is known as internationalization.In Selenium, we can modify the language preferences with the help of ChromeOptions class for the Chrome browser. We shall create an object of this class and apply addArguments method on it.To modify the language to Spanish, we have to pass −−lang=es as a parameter to the addArguments method. This information is then made available to the webdriver object.SyntaxChromeOptions opt = new ChromeOptions(); opt.addArguments("−−lang=es"); WebDriver drv = new ChromeDriver(opt);Exampleimport org.openqa.selenium.WebDriver; import ... Read More

Fire JavaScript Event in Selenium

Debomita Bhattacharjee
Updated on 02-Feb-2021 11:37:50

2K+ Views

We can fire JavaScript events in Selenium webdriver. Selenium can execute JavaScript events with the help of the JavaScript Executor. We shall pass the JavaScript commands as a parameter to the executeScript method.We shall fire the JavaScript event of clicking an element. First of all, we shall identify the element with the help of any of the locators like xpath, css, link text, and so on.We shall then pass argument [0].click() and webelement that shall be clicked as parameters to the executeScript method.SyntaxWebElement m = driver.findElement(By.linkText("Write for us")); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", m);Let us make try to click ... Read More

Send Keyboard Shortcut Alt+Shift+Z Hotkey with Selenium

Debomita Bhattacharjee
Updated on 02-Feb-2021 11:36:05

2K+ Views

We can send keyboard shortcut ALT SHIFT z(hotkey) with Selenium webdriver. This can be done with the help of the Keys class. We shall use the Keys.chord method and pass Keys.ALT, Keys.SHIFT and z as parameters to that method.The entire value obtained from the Keys.chord method is obtained as a String. That is then sent as a parameter to the sendKeys method.SyntaxString s = Keys.chord(Keys.ALT, Keys.SHIFT, "z"); driver.findElement(By.tagName("html")).sendKeys(s);ExampleCode Implementation with Keys.chord method.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class HtKeys{    public static void main(String[] args) {          System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       ... Read More

Finding Text on Page with Selenium

Debomita Bhattacharjee
Updated on 02-Feb-2021 11:35:54

803 Views

We can find text on page with Selenium webdriver. First of all, we shall identify the element with the help of the locator xpath. In xpath, we can use the contains() and text() functions.Let us find the below highlighted text on the page −The xpath expression shall be //*[contains(text(), 'You are browsing')]. To obtain the text of the element, the getText method is used. Let us check the xpath expression we have created from the Console tab with the expression: $x("//*[contains(text(), 'You are browsing')]").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 FindElmntsText{    public static void ... Read More

Does Selenium Support Safari Browser?

Debomita Bhattacharjee
Updated on 02-Feb-2021 11:35:32

954 Views

Yes Selenium webdriver supports Safari browser. Safari is a prominent browser and is provided by default by Apple devices. For Safari versions 10 and greater than 10, the safaridriver comes automatically and is not required to be installed separately.The location of the SafariDriver is: /usr/bin/safaridriver. Also, it must be remembered that to work with the Safari latest version, users should have the Apple machine. This is because the Apply no longer supports Safari on Windows (from 2012).If we are using an older version of Safari in the Apple machine, we have to turn on the webdriver support, by running the ... Read More

Change User Agent for Selenium Driver

Debomita Bhattacharjee
Updated on 01-Feb-2021 12:43:26

13K+ Views

We can change the user Agent for Selenium webdriver. The user Agent header has a particular string that provides the network protocol along with the details of operating system, software version, application, and so on.Selenium does have the ability to get or modify user Agent. This is done with the help of the JavaScript Executor. Selenium executes JavaScript commands with the help of the execute_script method.To obtain the user Agent information, we have to pass the return navigator.userAgent parameter to that method. To change the user Agent, we shall take the help of ChromeOptions class.Then apply the add_argument method on ... Read More

Get User-Agent Information in Selenium Web Driver

Debomita Bhattacharjee
Updated on 01-Feb-2021 12:42:59

4K+ Views

We can get the user Agent information with Selenium webdriver. This is done with the help of the JavaScript Executor. Selenium executes JavaScript commands with the help of the execute_script method.To obtain the user Agent information, we have to pass the return navigator.userAgent parameter to the execute_script method. Selenium does have a direct method the to get or modify user Agent.Syntaxa= driver.execute_script("return navigator.userAgent") print(a)Examplefrom selenium import webdriver from selenium.webdriver.chrome.options import Options #object of Options class op = webdriver.ChromeOptions() #set chromedriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", options=op) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.seleniumhq.org/download/"); #get user Agent with execute_script a= driver.execute_script("return navigator.userAgent") print("User ... Read More

Set Up a Real Timeout for Loading Page in Selenium WebDriver

Debomita Bhattacharjee
Updated on 01-Feb-2021 12:42:10

4K+ Views

We can set a real timeout for loading pages in Selenium webdriver. 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 ... Read More

Use Chrome WebDriver in Selenium to Download Files in Python

Debomita Bhattacharjee
Updated on 01-Feb-2021 12:41:55

1K+ Views

We can use chrome webdriver in Selenium to download files in Python. We shall use the ChromeOptions class for this purpose. First, we shall create an object of the ChromeOptions class.Then apply the add_experimental_option method on the object created. We shall set the download.default_directory: parameter. Finally, this information shall be passed to the driver object.Syntaxop = webdriver.ChromeOptions() p = {'download.default_directory':'C:\Users\ghs6kor\Downloads\Test'} op.add_experimental_option('prefs', p)Examplefrom selenium import webdriver from selenium.webdriver.chrome.options import Options #object of Options class op = webdriver.ChromeOptions() #browser preferences p = {'download.default_directory':'C:\Users\ghs6kor\Downloads\Test'} #add options to browser op.add_experimental_option('prefs', p) #set chromedriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", options=op) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.seleniumhq.org/download/"); #click ... Read More

Get Selenium to Wait for AJAX Response

Debomita Bhattacharjee
Updated on 01-Feb-2021 12:37:47

4K+ Views

We can get Selenium to wait for Ajax response. The determination of the load time of the page due to an Ajax response is a difficult task. This can be achieved with the help of synchronization concepts and wait methods in Selenium are −Implicit wait − It allows the web driver to wait for the time specified after which exception is thrown. This wait is applicable to the all the steps in the test.Syntaxdriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);ExampleCode Implementation with implicit wait.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; public class AjaxImplWt{    public static void main(String[] args) {     ... Read More

Advertisements