protractor icon indicating copy to clipboard operation
protractor copied to clipboard

I am not able to click on element using any of the locators only javascript executor works for me in angular 2 pages

Open Shobysukumaran opened this issue 9 years ago • 10 comments

Here is my html for angular 2 applications. I am executing my tests in chrome

<md-card-title _ngcontent-c10="" class="ra-card-title mat-card-title">
      <a _ngcontent-c10="">Massachusetts Division of Banks Issues Decision Establishing Limit on Maximum Allowable Fee for Dishonored Checks for State - Chartered Financial Institutions</a>
    </md-card-title>
<div _ngcontent-c10="" class="ra-card-header__footer">
      <md-card-subtitle _ngcontent-c10="" class="ra-card-code mat-card-subtitle">BLN3594</md-card-subtitle>
      
      <div _ngcontent-c10="" class="ra-card-menu">
        <span _ngcontent-c10="" aria-haspopup="true" class="ra-card-menu-button">...</span>
        <md-menu _ngcontent-c10="" role="menu" xposition="before" yposition="below"><!----> </md-menu>
      </div>
    </div>

<md-card-subtitle _ngcontent-c10="" class="ra-card-code mat-card-subtitle">BLN3594</md-card-subtitle>

I am not able to click on the menu button using

element(by.xpath("//span[@class='ra-card-menu-button'])[0]")).click();

But if I use below javascript executor , it is working fine.

var scrpt = "return document.getElementsByClassName('ra-card-menu-button')[0].click();";
        browser.executeScript(scrpt).then(function (text) {
            logger.log('info', 'Script is: ' + scrpt);

        });

protractor version is 5.1.1.

Could anyone please help me to identify the issue

Shobysukumaran avatar May 17 '17 05:05 Shobysukumaran

In the first example you are using xpath and then in the second one you are using class to find your element. Have you tried to identify it with css? element(by.css('.ra-card-menu-button'))?

Please ask your support questions on StackOverflow, Google Group discussion list, or Gitter. For more information please reference https://github.com/angular/protractor/blob/master/CONTRIBUTING.md#questions

Thank you!

cnishina avatar May 17 '17 07:05 cnishina

Yes , I have tried element(by.css('.ra-card-menu-button')). BUt no luck.

I am able to work only through javascript

Shobysukumaran avatar May 17 '17 10:05 Shobysukumaran

If i understand correctly , in the first statement you are tyring to select all elements and in the second i see you have given the index. I believe you should be using the following and hope it works. : element.all(by.css('ra-card-menu-button'])).click();

And to chose one you can use the index.

SrivaniA avatar May 17 '17 12:05 SrivaniA

I tried like below: element.all(by.css('ra-card-menu-button')).then(function(ele){ var open=ele[0]; open.click(); })

But Getting the followng error " - Failed: Cannot read property 'click' of undefined "

Shobysukumaran avatar May 17 '17 13:05 Shobysukumaran

Because by CSS means that you need to provide a . for classes and a # for ID's. Now you are looking for the TAG ra-card-menu-button, you should use element(by.css('.ra-card-menu-button')).click();

Don't then() here

wswebcreation avatar May 17 '17 15:05 wswebcreation

I have the same issue:

When('I write {val} inside {inputBoxName} input box', (val: string, inputBoxName: string, callback) => {

  let inputbox = element(by.css('#roomWidthInput'));
  console.log('inputBox promise set');

  var scrpt = "return document.getElementById('roomWidthInput');";
  browser.executeScript(scrpt).then(function (text) {
    console.log('info', 'Script is: ' + scrpt);
  });

  inputbox.isPresent().then(function(isElementVisible) {
    console.log('hello!');
    expect(isElementVisible).to.be.true;
    callback();
  });
});

The console logs: inputBox promise set info Script is: return document.getElementById('roomWidthInput');

and then it throws Error: function timed out after 5000 milliseconds. Any help would be really appreciated.

surfaslt avatar Mar 28 '18 11:03 surfaslt

@Shobysukumaran Did you get any resolution for your issue ? I am facing same issue after upgrading Chrome. Normal click() function is not working. I have to use javascript executor to click.

browser.executeScript("arguments[0].click();", )

stemkar123 avatar May 16 '18 06:05 stemkar123

@stemkar123 Try this method When('I write {val} inside {inputBoxName} input box', (val: string, inputBoxName: string, callback) => {

let inputbox = element(by.css('#roomWidthInput')); console.log('inputBox promise set');

var scrpt = "return document.getElementById('roomWidthInput');"; browser.executeScript(scrpt).then(function (inputBox) { console.log('info', 'Script is: ' + scrpt); inputBox.isPresent().then(function(isElementVisible) { console.log('hello!'); console.log((isElementVisible) ); expect(isElementVisible).to.be.true; callback(); }); });

});

Shobysukumaran avatar May 16 '18 06:05 Shobysukumaran

@stemkar123 In the success of execute script I have entered the code inputBox.isPresent().then(function(isElementVisible) { console.log('hello!'); console.log((isElementVisible) ); expect(isElementVisible).to.be.true; callback(); });

Shobysukumaran avatar May 16 '18 07:05 Shobysukumaran

@Shobysukumaran Actually we have 400+ tests in out framework & it is extremly difficult to go & change each & every script to update the click(). :( Is there anything that I should update apart from Script.

stemkar123 avatar May 16 '18 07:05 stemkar123