简体   繁体   English

如何在量角器中查找元素定位器

[英]how to Find element Locator in Protractor

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. 我是量角器的新手,请使用我尝试使用xpath和CSS的Protractor / angularJS中的以下代码来帮助我找到元素定位器。 what locator can be used from this code 可以从此代码中使用什么定位器

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

Here are various methods you can use apart from xpath and css. 除了xpath和CSS之外,还有多种方法可以使用。 Locators specific to Protractor - 量角器专用的定位器-

  1. Get element using buttonText . 使用buttonText获取元素。

     element(by.buttonText('Reset')); 
  2. You can also add a custom locator for your button using addLocator in protractor. 您也可以使用addLocator中的addLocator为按钮添加自定义定位器。 Here's an example of it . 这是一个例子

  3. Use cssContainingText to get the element using both css and text properties. 使用cssContainingText可以同时使用css和text属性获取元素。

     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. 如果您的按钮元素中有一个较长的文本(例如,“重设此值”),请使用partialButtonText来获取它。

     element(by.partialButtonText('Value')); 
  5. You can also use a shortcut to find elements using css. 您还可以使用快捷方式使用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 - 使用className获取元素-

     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 - 使用tagName来获取元素(如果它是页面上的唯一按钮元素),这种情况很少见-

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

Hope it helps. 希望能帮助到你。

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

以上答案解决了。

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. 对于量角器,最好使用elementor(elementor URL)来查找元素定位符。 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 command prompt after starting the protractor. 启动量角器后,命令提示符中的elementor URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM