Using protractor to find and click an anchor link

12,088

Solution 1

You might need to wait until you are on the target page. There is the relevant urlIs Expected Condition added to the Protractor 4:

var nortonlink = $('[href*=safeweb]');  // NOTE: also simplified the selector
nortonlink.click();

var EC = protractor.ExpectedConditions;
browser.wait(EC.urlIs("https://safeweb.norton.com/"), 5000);

I'm not sure if you need the trailing slash here - try with and without.

Solution 2

Use protractor.ExpectedConditions to check whether link clickable or not. and then click on it.

   var EC = protractor.ExpectedConditions;

   var nortonlink = element(by.css('[href="//safeweb.norton.com/"]'));

   nortonlink.click().then(function(){
   //wait until page completely  loaded
   browser.wait(function () {
       return browser.getCurrentUrl().then(function (url) {
                if(url==!"old url"){       
                return true;
                } });
      }, 8000, 'URL has not changed');

   expect(browser.getCurrentUrl()).toEqual('https://safeweb.norton.com/');
   });
Share:
12,088
Jim Moak
Author by

Jim Moak

Updated on June 08, 2022

Comments

  • Jim Moak
    Jim Moak almost 2 years

    Should be simple, I am trying to click on an image link using Protractor.

        <a href="//safeweb.norton.com
        /a>
    

    I know I'm doing it the long way.

        var nortonlink = element(by.css('[href="//safeweb.norton.com/"]'));
    
        nortonlink.click();
    
        expect(browser.getCurrentUrl()).toEqual('https://safeweb.norton.com/');
    

    My expect keeps saying it is still on the current page, so the click isn't operating.