Wednesday, June 19, 2024

  

Handling disable Elements using JSE :

-----------------------------------

By using Selenium , we cannot handle disable elements.---->  Go to JSE

if element is disabled, we can interact with that element  using JSE

ex: if text box is disabled, We can enter data into textbox using JSE .

ex: if checkbox is disabled, we can click checkbox using JSE..

----------------------------

package Java Script executor Basics;


import org. openqa. selenium. By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class HandlingDisableElementsUsingJSE {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

// //open chrome browser

WebDriver  driver =  new ChromeDriver();

driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

//  click check box  'I have an aeroplane'

// driver.findElement(By.xpath("//input[@id='bikeid'][@name='bike'][@value='Car']")).click();

//   not throwing any errror  -- not clicking checkbox as it is disabled

//  Note :   it does not throw any error (or) exception if we click element which is disabled

// By using Selenium, we cannot click disabled element


/// JSE

// IF ele is disabled , we can handle those elements using JSE

// JS code: document.getElementsByName('bike')[2].click(); --  >  not clicking checkox

// JSE: document.getElementsByName('bike')[2].checked = true;  --- >   working 

// HW 

//HW  click 'Computer '  check box use id using JSE


// enter some data in 'ename ' text box -- disabled 

// driver.findElement(By.name("myname")).sendKeys("Raju");// Excpetion

// " org.openqa.selenium.ElementNotInteractableException: element not interactable


// HW Enter data in 'ename' text box - disabled  using JSE

// JSE: document.getElementsByName('myname')[0].value = "Raja";// ok

// document.getElementsByName('myname')[0].value = 'Raja';// ok

System.out.println("ends");


}


}


How to use CSS Selector in Javascript executor

 How to use CSS Selector in Javascript executor:

--------------------------------------------

querySelector()  & QuerySelectorAll UsingJSE : --> learn 

----------------------------------------------

In querySelector()  & QuerySelectorAll, we have to pass CSSSelector  and identifies element


querySelector("CSS Selector")  =  findElement(By.cssSelector(""))

querySelectorAll("CSS Selector")  =   findElements(By.cssSelector(""))




// Enter 'Sita' in first name textbox  using Css and JSE 

JS CODE : document.querySelector("input[name='firstname']").value = 'Raju';

'Raju'


//HW  get all textboxes count using CSSSelector using JSE

// querySelectorAll()  -  we pass CSS selector only and identifies multiple elements or list of webelements from page

js CODE: -->  document.querySelectorAll("input[type='text']").length;


// HW get first text box and enter some data  using querySelector()


//HW  get 2nd text box and enter some data using querySelectorAll()


Note:  

 we cannot write x'path in JavaScript code..But We can write CssSelecors in Javascript code in querySelector() , querySelectorAll() 


querySelector("CSS Selector") 

querySelectorAll("CSS Selector") 


Handling disable elements using JSE


Handling Dropdown using JSE

 Handling Dropdown using JSE:

---------------------------


Select tag  -- Class  --> Select -class 

                           selectByvisibleText()

   selectByvalue();

                   selectByIndexNo();


   

package JavaScriptexecutorBasics;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class DroddownHandlingUsingJSE {


public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");


// //open chrome browser

WebDriver  driver =  new ChromeDriver();

driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");


// if dropdown is developed by using 'Select' tags ---  'Select' class methods

// SelectbyVisibleText() , Selectbyvalue(), SelectbyIndex();

//  some times, these methods will not work



// select first option in 'cars' dropdown using indexno

// JS code--> document.getElementsByName('cars')[0].selectedIndex = 0;

//HW js code


Thread.sleep(3000);


//HW  select 'Maruthi'    based on index no ?



Thread.sleep(3000);


//HW select 2nd option in cars dropdown using

//js code--> 

// select Swiftvdi


// HW select 3rd option in cars dropdown using index no


// HW select 4th option in cars dropdown using index no


//   get selected value indexno  from dropdown

//JS Code--> 

//HW 



//   get dropdown values count :

// JS code: document.getElementsByName('cars')[0].options.length;// 6

// HW 



// Get first dropdown Value :i.e Maruthi

// JS code:  document.getElementsByName('cars')[0].options[0].innerText;

// HW 


// HW get 2nd, 3rd  dropdown value SwiftVDI, Mercedes



//HW  WAP get all dropdown values -

// Js code :document.getElementsByName('cars')[0].options[0].innerText;

//     Js code :document.getElementsByName('cars')[0].options[1].innerText;

//     js code :document.getElementsByName('cars')[0].options[2].innerText;

//       ........................                          

//..........................................................5

 


// way2:  Select dropdown value using by value 

//JS code  document.getElementsByName('cars')[0].value= 'Swiftvdival';

//HW 


// HW  select diff  Mercedes, audi, BMW  using value ?



// HW  Select dropdown value using visible text ???????????????  Search ??? not any method




}


}



Handling Link using JSE

 

Handling Link using JSE:

package JavaScriptexecutorBasics;


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class LinkHandlingByJSE {


public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");


// //open chrome browser

WebDriver  driver =  new ChromeDriver();

driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");


// Links count - tag -- <a href= "google.com" >  My google  </a>

//JS code --> document.links.length;

//  get 1st link --> document.links[0];

// get 2nd links--> document.links[1];

//JSE:

JavascriptExecutor jse  = (JavascriptExecutor)driver;

Object linksCnt= jse.executeScript("return document.links.length;");

System.out.println("linksCnt="+linksCnt);


//HW  get 1st link  url 

//  JS code--> document.links[0].href;

//



// HW get 2nd  link url?



// get text of 1st link i.e ToolTip Sumanth -innerText

// Js code --> document.links[0].text;

//HW

//HW  get text of 2nd link i.e ToolTip Link


// click   1st link

//JS code --> document.links[0].click();

//HW 


//HW  get all link  names and url using for loop using JSE 

// document.links[0].innerText;

// document.links[1].innerText;

// document.links[2].innerText;

// ..................

///.............. 8


////history.back();  ==  driver.navigate().back();

// history.forward();  ==  driver.navigate().forward();

// HW  :   JSE 



}


}


Unable to click button using Selenium -Java (or) Handling button using JSE

 Handling button using JSE:

Handling a button using JavaScript Executor (JSE) in Selenium is similar to handling a checkbox. You can use the executeScript method to interact with the button directly through the DOM. 


public class BtnHandlingByJSE {


public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");


// //open chrome browser

WebDriver  driver =  new ChromeDriver();

driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");


//  click 'MySubmit' button using  id 

// driver.findElement(By.id("MyButton")).click();// assume that is not working


/// JSE

JavascriptExecutor jse = (JavascriptExecutor)driver;


//  click 'MySubmit' button using  id  using JSE

// JS code use id --> document.getElementById('MyButton').click();

// .  use the above code, call executeScript();

jse.executeScript("document.getElementById('MyButton').click();");


// get button 'type' attribute val  i.e submit

// driver.findElement(By.id("MyButton")).getAttribute("type");// submit

//JS code use id :  document.getElementById('MyButton').type;

// HW  use above cose , call executescript ()

Object oType =  jse.executeScript("return document.getElementById('MyButton').type;");

System.out.println("oType="+oType);


//HW get 'id' value of My submit button i.e MyButton using JSE

// Ans : MyButton

// JS code:


// Hw get 'value' property of My submit button i.e 'My Submit' using JSE

// ans: 'My Submit'

// JS code --> 


// HW click 'login' button using JSE



}


}


git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...