Get Selected Option Using Selenium WebDriver with Python

Debomita Bhattacharjee
Updated on 18-Sep-2020 13:25:05

5K+ Views

We can obtain the option selected in a dropdown with Selenium webdriver. The first_selected_option method fetches the option selected in the dropdown. Once the option is returned we need to apply a text method to get option text.Let us consider the select dropdown Continents and fetch its selected item −Examplefrom selenium import webdriver from selenium.webdriver.support.select import Select import time driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm") # identify dropdown s = Select(driver.find_element_by_xpath("//select[@name='continents']")) #select by option index s.select_by_index(4) #get selected item with method first_selected_option o= s.first_selected_option #text method for selected option text print("Selected option is: "+ o.text) driver.close()OutputRead More

Store Count of Digits in Order Using JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:24:34

114 Views

Suppose we have a string with digits like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.Therefore, for this string, the output should be −const output = {    "1": 2,    "2": 4,    "3": 3,    "4": 7,    "5": 1,    "6": 3 };ExampleFollowing is the code −const str = '11222233344444445666'; const mapString = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] ... Read More

PHP require_once Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:23:49

388 Views

IntroductionThe require_once statement in PHP is similar in functioning to that of require statement. Only difference is, if a file has been already included for processing, it will not be included again.Note that a file included with either include or require statement will also be not included again even if require_once statement is used.Other behaviour of require_once statement is similar to require statement .require_once ExampleIn following example main php script includes test.phpExample Live Demo //test.php OutputThis will produce following result when main script is run from command line −inside main script now including with require_once test.php script File included try to include ... Read More

Find the Sum of Unique Array Values in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:23:33

991 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers.Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array.For example −If the input array is −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11];Then the output should be 25We will simply use a for loop, iterate the array and return the sum of unique elements.ExampleFollowing is the code −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; const ... Read More

Add Values of Matching Keys in Array of Objects in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:22:25

658 Views

Suppose we have an array of objects like this −const arr = [{a: 2, b: 5, c: 6}, {a:3, b: 4, d:1}, {a: 1, d: 2}];Each object is bound to have unique keys in itself (for it to be a valid object), but two different objects can have the common keys (for the purpose of this question)We are required to write a JavaScript function that takes in one such array and returns an object with all the unique keys present in the array and their values cumulative sum as the valueTherefore, the resultant object should look like −const output = ... Read More

PHP Require Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:22:25

311 Views

IntroductionEffect of require statement is similar to include statement in PHP. However, there is one main difference. If the parser fails to find required file, it produces a fatal error thereby termination of current script. The include statement on the other hand emits a warning in case of failure to find file and execution of current script continues.PHP parser tries to locate the file in current folder by default and further in directories mentioned in include_path setting of php.ini, as in case of include statement. If file asked for is not available in current folder as well as include_path folders, ... Read More

Sorting Objects by Numeric Values in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:21:13

463 Views

Suppose we have an object like this −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 };We are required to write a JavaScript function that takes in this object and returns a sorted array like this −const arr = [11, 23, 56, 67, 88];Here, we sorted the object values and placed them in an array.ExampleFollowing is the code −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 }; const sortObject = obj => {    const arr = Object.keys(obj).map(el => {       return obj[el];    });    arr.sort((a, b) => {       return a - b;    });      return arr; }; console.log(sortObject(obj));OutputThis will produce the following output in console −[ 11, 23, 56, 67, 88 ]

Convert JS Array into an Object in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:19:56

334 Views

Suppose, we have an array of objects like this −const arr = [    {id: 1, name: "Mohan"},    {id: 2, name: "Sohan"},    {id: 3, name: "Rohan"} ];We are required to write a function that takes one such array and constructs an object from it with the id property as key and name as valueThe output for the above array should be −const output = {1:{name:"Mohan"}, 2:{name:"Sohan"}, 3:{name:"Rohan"}}ExampleFollowing is the code −const arr = [    {id: 1, name: "Mohan"},    {id: 2, name: "Sohan"},    {id: 3, name: "Rohan"} ]; const arrayToObject = arr => {    const ... Read More

Use Select List in Selenium

Debomita Bhattacharjee
Updated on 18-Sep-2020 13:19:12

3K+ Views

We can select an option from the dropdown list with Selenium webdriver. The Select class is used to handle static dropdown. A dropdown is identified with the tag in an html code.Let us consider the below html code for tag.We have to import org.openqa.selenium.support.ui.Select to work with methods under Select class in our code. Let us see some of the Select methods−selectByVisibleText(arg) – An option is selected if the text visible on the dropdown is the same as the parameter arg passed as an argument to the method.Syntaxsel = Select (driver.findElement(By.id ("option")));sel.selectByVisibleText ("Selenium");selectByValue(arg) – An option is selected ... Read More

Sum Arrays with Repeated Values in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:18:44

130 Views

Suppose, we have an array of objects like this −const arr = [    {'ID-01':1},    {'ID-02':3},    {'ID-01':3},    {'ID-02':5} ];We are required to add the values for all these objects together that have identical keysTherefore, for this array, the output should be −const output = [{'ID-01':4}, {'ID-02':8}];We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the array.ExampleFollowing is the code −const arr = [    {'ID-01':1},    {'ID-02':3},    {'ID-01':3},    {'ID-02':5} ]; const indexOf = function(key){   ... Read More

Advertisements