Saturday, June 22, 2024

Handling Link

 

Handling Link:

Common Actions:

  1. Click the Link
  2. Get the URL of the Link
  3. Check if the Link is Displayed
  4. Check if the Link is Enabled
  5. Get the Text of the Link

Sample HTML:

<a href="https://www.google.com" >Click My Google</a>

 

Example Code:

package WebelementsBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class LinkBasics1 {

 

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

 

                        //open chrome browser

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

                       

                        WebDriver  driver  = new ChromeDriver();

                        // Note: import all predefined classes and interfaces --> ctrl + shft+ o

 

 

                        // open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html

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

 

 

                        // click 'My google' link

                        //                      1. Identify ele 2. what action to perform - click()

//                      driver.findElement(By.id(""));

//                      driver.findElement(By.name(""));

//                      driver.findElement(By.className(""));

                        // -- no name

                        // -- no id

                        //By.className("");// -- no class                   

                        Thread.sleep(3000);

                        //click 'My google'  link   By.linkText          

//                      driver.findElement(By.linkText("Click My Google")).click();

//                      driver.findElement(By.linkText("Click My Google123")).click();

 

                        // if no link text is there in page, ???

                        //  it goes to html page ,  find the ele   "by class" locator i.e link text ="Click My Google1234"

                        //   if  there is no match found in html page, it throws NoSuchElementException

 

                        //                                  Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Click My Google1234"}

 

 

                        // get url name from link -"Click My Google"

                        // href val=https://www.google.com/

                        String url = driver.findElement(By.linkText("Click My Google")).getAttribute("href");

                        //                     https://www.google.com/

 

                        // url =  https://www.google.com/

                        System.out.println("googleHrefAttr="+ url);

                        //

                       

                        //  get link text for 'click My google' link

                       

 

                        //       Linktext=

                        //  use gettext()  for  <a> text  <a>

                        //                       <p>  para-1 </p>

                        //                     <option>  Honda </option>

                        String Linktext =  driver.findElement(By.linkText("Click My Google")).getText();

                        //                             Click My Google

                        // Linktext  =  Click My Google

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

                        //                          Click My Google

                       

 

                        // HW  get url of   'Click My Facebook'  link

 

                        // HW Check "Click My Facebook'  link  is displayed, is enabled

 

                        // HW  Click 'Click My Facebook'   and go back to previous page , 

                       

                        //HW  get url  of 'Open Face book Link'  by using href attribute name

 

 

 

            }

 

}

 

Handling Link using PartialLinkText:

 

package WebelementsBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class LinkBasics2 {

 

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

                        //open chrome browser

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

                        WebDriver  driver =  new ChromeDriver();

 

                        Thread.sleep(3000);

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

 

                        // clikc 'click  My google  ' link

                        //                      1. Identify ele 2. what action to perform - click()

//                                              driver.findElement(By.name(""));// -- no name

//                                              driver.findElement(By.id("")) ;// -- no id

//                                              By.className("");// -- no class

                                               

//                                  WebElement MygoogleLinkele=             driver.findElement(By.linkText("Click My Google"));//                                                     

//                                  MygoogleLinkele.click();

                       

                       

//                      By.partialLinkText("partial link text");

//                      By.partialLinkText("My Google");

//                      By.partialLinkText("Google");

 

                        // find element using by partialLinkText("My Google")

                        driver.findElement(By.partialLinkText("My Google")).click();

                                                                                   

                        // HW  if we pass invalid link text

//                                  Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Click My Google1234"}

 

                                   

                        //HW  get url name from  "click My google "link using partialLinkText()//                                        

//                                  // href val=https://www.google.com/

                                   

                                   

                        // HW  get url of   'Click My Facebook'  link using partialLinkText()

 

                        // HW Check "Click My Facebook'  link  is displayed, is enabled using partialLinkText()

 

 

            }

 

}

No comments:

Post a Comment

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...