最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to Find element Locator in Protractor - Stack Overflow

programmeradmin3浏览0评论

Iam new to protractor, Please help me to find the element locator using below code in Protractor/angularJS i have tried using xpath and CSS. what locator can be used from this code

<button type="submit" value="autofocus" class="btn" data-ng-click="reset()">Reset</button>

Iam new to protractor, Please help me to find the element locator using below code in Protractor/angularJS i have tried using xpath and CSS. what locator can be used from this code

<button type="submit" value="autofocus" class="btn" data-ng-click="reset()">Reset</button>
Share Improve this question edited Feb 4, 2016 at 9:41 giri-sh 6,9622 gold badges27 silver badges50 bronze badges asked Sep 30, 2015 at 6:54 LokeshLokesh 2142 silver badges11 bronze badges 2
  • Try this: element(by.css('.btn')) – Niezborala Commented Sep 30, 2015 at 7:02
  • thanks for time but the thing is element(by.cssContainingText('.btn', 'Reset')); worked. ur code didnt show be error but it didnt even click the element. – Lokesh Commented Sep 30, 2015 at 7:23
Add a ment  | 

4 Answers 4

Reset to default 5

Here are various methods you can use apart from xpath and css. Locators specific to Protractor -

  1. Get element using buttonText.

    element(by.buttonText('Reset'));
    
  2. You can also add a custom locator for your button using addLocator in protractor. Here's an example of it.

  3. Use cssContainingText to get the element using both css and text properties.

    element(by.cssContainingText('.btn', 'Reset'));
    
  4. If at all you have a longer text in your button element(for ex, "Reset This Value"), then use partialButtonText to get it.

    element(by.partialButtonText('Value'));
    
  5. You can also use a shortcut to find elements using css.

    $('.btn'); // = element(by.css('.btn'))
    

More details of the locators in Protractor are here.

You can use various General locators too -

  1. Use className to get the element -

    element(by.className('btn'));
    
  2. Use tagName to get the element if its the only button element on the page, which is a rare case -

    element(by.tagName('button'));
    

Hope it helps.

element(by.cssContainingText('.btn', 'Reset'));

Above answer worked out.

try the following:

element(by.className("btn"));     

or

$('.btn');

or

element(by.css("btn"));

it is better to use elementor (elementor URL) for protractor to find element locator. this will help u in finding the exact locator without any issues. but little bit time consuming. but u dont need to worry on the elements found.

elementor URL in mand prompt after starting the protractor.

发布评论

评论列表(0)

  1. 暂无评论