Found 6710 Articles for Javascript

Reading JavaScript variables using Selenium WebDriver.

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

3K+ 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

Running javascript in Selenium using Python.

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

730 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

Getting the return value of Javascript code in Selenium.

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

4K+ Views

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

Match specific word in regex in JavaScript?

Nikhilesh Aleti
Updated on 22-Sep-2022 12:05:51

11K+ Views

The task is to match a specific word or character which is in regex with a string. The regex (regular expressions) is a pattern that is used to match character combinations in strings. Here we include the test(), match(), and matchAll() methods to match the following word in regex. We have some boundary-type assertions, in which we have used \b. Consider a sentence – “mickey is holding mic.” Using regex - \bmic\b will match the word mic but not the word mic in mickey. It is a word boundary. Another assertion is (g), It is a global search flag. Consider ... Read More

Get price value from span tag and append it inside a div after multiplying with a number in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:33:33

1K+ Views

Extract the value and convert it from String to integer to get price value from span tag.ExampleFollowing is the code −            Document           Value=                10                    $(document).ready(function () {       var v = parseInt($(".purchase.amount")       .text()       .trim()       .replace(', ', ''));       var totalValue = v * 10;       console.log(totalValue);   ... Read More

Removing listener from inside outer function in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:28:41

92 Views

To remove listener from outer function, use removeEventListener().ExampleFollowing is the code − Document Press Me    var demoId = document.getElementById('demo');    demoId.addEventListener('click', function fun() {       outerFunction(this, fun);    }, false);    function outerFunction(self, funct) {       console.log('outer function is called....');       self.removeEventListener('click', funct, false);       console.log("Listener has been removed...")    } To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.OutputThis will ... Read More

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
Updated on 24-Oct-2020 12:23:58

242 Views

To merger properties of two objects, you can use the concept of {...object1,...object2,... N).ExampleFollowing is the code −var firstObject = {    firstName: 'John',    lastName: 'Smith' }; var secondObject = {    countryName: 'US' }; var mergeObject = {...firstObject, ...secondObject}; console.log(mergeObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo307.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo307.js { firstName: 'John', lastName: 'Smith', countryName: 'US' }

How to get all combinations of some arrays in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:22:27

315 Views

You can use your own function to get all combinations.ExampleFollowing is the code −function combination(values) {    function * combinationRepeat(size, v) {       if (size)          for (var chr of values)       yield * combinationRepeat(size - 1, v + chr);       else yield v;    }    return [...combinationRepeat(values.length, "")]; } var output = combination([4,5]); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo306.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo306.js [ '44', '45', '54', '55' ]

JavaScript/ Typescript object null check?

Nikhilesh Aleti
Updated on 18-Jun-2024 17:36:07

14K+ Views

In this article we will check if an object is a null in Typescript. A variable is undefined until and unless it is not assigned to any value after declaring it. NULL is known as empty or dosen’t exist. In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator. Using typeofoperator The typeof operator in JavaScript is used to find the datatype of the variable. Example In the example ... Read More

Recursion example in JavaScript to display numbers is descending order?

Nikhilesh Aleti
Updated on 08-Nov-2022 08:20:18

937 Views

In this article, the given task is to display the numbers in descending order by using recursion. Let’s get an understanding of the task by looking into the input-output scenarios. Input-Output scenario Let’s look into an input-output scenario, where there is an input number and we need to print the numbers in descending order from the given number to 1. Input = 10; Output = 10 9 8 7 6 5 4 3 2 1 What is recursion? The process of calling itself is called recursion. The function which will call itself is known as a recursive function. Syntax Following ... Read More

Advertisements