ajay kumar
ajay kumar

Reputation: 73

Calling href value in Selenium Webdriver

Could you please help me to call href value which is changing every time. Below is the code for your reference:

<a href="http://click1.clickrouter.com/redirect?token=a2258079c24c4c50a56b6b1ffb75d6e2&amp;url=https%3A//u2720269.ct.sendgrid.net/wf/click%3Fupn%3DRuyytLyMzocaRpUHe9Z1QCe0o5SY-2BbrKEnssVFEC2nFM466GSvGSbWIPZo2Xpeyn-2F0QAElxl7c7bwKXcdymgtcm9RjSv0ivZZXtv2n8DKYnWlKkQroF-2BEDfllIkFXTyS20elE5OuRKl3wPQBh0E76Nj-2BQArVjoxEfD4fUEVLUkI-3D_5SnvLCTBgVqPjimwaiavQTZsy6v0FmqoHbMtHV6MMfx-2FCs1eGI1d4Bexl3L2zftKx-2FvaNVM5Uwe7h0ZTAHWuyNOLa3UkvoB3WljrbHWRfL2LCArojXFquIo8ltK6-2BRTtQyTe7jpRGsBnxQUyyEhyT7KGbgdF0T-2BqZ01kZMoHzklNV-2BOkV6gSY0NZvlW0iq6LXowmHug1UsgSa4LTxTH0elpPaEGQhdEPe0zJsuoYSBQ-3D" target="_other" rel="nofollow">click here</a>

Thanks.

Upvotes: 0

Views: 1238

Answers (1)

Sagar007
Sagar007

Reputation: 848

As I understand from your comment, Test cases needed the href (link) attribute value. So code can be written by this way :

 String strLinkHref = driver.findElement(By.linkText("click here")).getAttribute("href");

or

String strLinkHref = driver.findElement(By.xpath("//a[text()='click here']")).getAttribute("href");

Note : here you can store in String and print. It will get link dynamically every time.


If test case needed to open it, then you can use :

driver.get(strLinkHref);

If you require to move to TAB window, Please use below code :

     String handle= driver.getWindowHandle(); 

     System.out.println(handle); 

     // Click on the Button "New Message Window" 

     driver.findElement(By.name("New Message Window")).click(); 

     // Store and Print the name of all the windows open 

     Set handles = driver.getWindowHandles(); 

     System.out.println(handles); 

     // Pass a window handle to the other window 

     for (String handle1 : driver.getWindowHandles()) { 

     System.out.println(handle1); 

      driver.switchTo().window(handle1); 
      currentURL = driver.getCurrentUrl();
      System.out.println(currentURL);

     } 

     // Closing Pop Up window
      driver.close();

Reference

Upvotes: 1

Related Questions