【发布时间】:2018-03-22 01:24:28
【问题描述】:
我想按类名而不是 css 或 id 选择对象,但我不断收到错误,无法找到 css 选择器,即使我尝试使用类名进行选择。甚至说我也无法使用 css 获取元素。我刚刚设置了量角器,所以也许我错过了什么? protractor - 版本 5.3.0,我从命令行使用“ng e2e”运行测试文件。
这是我的页面对象:
import { browser, by, element } from 'protractor';
export class AppPage {
navigateTo() {
return browser.get('https://mywebsite.com');
}
getSearchBtn() {
return element(by.className('c-search-form-button'));
这是我的规范文件
import { AppPage } from './app.po';
import { browser, by, element } from 'protractor';
import {Config} from 'protractor';
describe('playground', () => {
let page: AppPage;
;
beforeEach(() => {
page = new AppPage();
browser.waitForAngularEnabled(false); // must have if non-angular site
page.navigateTo(); // or browser.get(url); //or browser.get('https://website.com');
});
it('click the search button', () => {
page.getSearchBtn().click();
});
});
错误
- Failed: No element found using locator: By(css selector, .c-search-form-button)
对象
<div class="c-search-form">
<push-button class="c-search-form-button">
...
</push-button>
我也试过
element(by.className('c-search-form'));
element(by.className('c-search-form-button'));
element(by.css('.c-search-form-button'));
element(by.css('.c-search-form'));
element(by.tagName('push-button'));
【问题讨论】:
-
没有足够的信息来重现...见how to ask
-
这个类'c-search-submit-button'是否真的存在于你的代码中?我可以想象无实体的类被 CSS 忽略,因此量角器看不到......代码本身看起来不错。
-
必须有更多这个。我完全照原样复制了您的代码,它给了我一个
Element is not clickable at point (x, y)消息。当我在 div 中添加button时,测试变为绿色。你的代码似乎没问题。还有其他原因导致失败。您确定在尝试单击按钮之前导航到了正确的页面吗? -
我已经用更多代码更新了我的示例。请任何关于我哪里出错的指示。
-
尝试在
page.getSearchBtn().click()之前添加一些睡眠browser.sleep(15*1000) // 15 secs。而如果元素在frame里面,需要先切换到frame里面。
标签: angular protractor