我如何找到元素<a> and click

时间:2017-11-13 17:44:33

标签: javascript html jasmine protractor

Can someone help me how do i find and click on it using Protractor

<div class="col-md-3 col-sm-6 col-xs-8">
    <div class="nav-control pull-right">
            <li>
                <a class="btn theme-button" ng-click="loginSignUp()">Login/Signup</a>
            </li>
        </ul>
    </div>
</div>

My attempts but none of them successfully so for.

$('.nav-control pull-right a').click();

var li = element(by.xpath('//ul/li/a'));
expect(li.getText()).toBe('Doge meme');


// by class name
element(by.className('btn theme-button')).click();

// by css
element(by.css('.btn')).click();
element(by.xpath('.//*[.="Login/Signup"]')).click();
element(by.xpath('//*[@id="top"]/div[4]/div/div/div[1]/div/div[1]/div[3]/div/ul/li/a')).click();

Update:

I am able to click the tag with following code.

  let el = element(by.cssContainingText('a', 'Login/Signup'));      
  var EC = protractor.ExpectedConditions;      
  browser.driver.wait(function () {
      browser.wait(EC.visibilityOf(el), 10000);
      browser.driver.manage().window().setSize(1280, 1024);
      el.click();
  });

However, even it clicks the anchor tag but still complains in the console

Failed: unknown error: Element ... is not clickable at point (1164, 27). Other element would receive the click: ... (Session info: chrome=61.0.3163.100) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)

2 个答案:

答案 0 :(得分:1)

有几种方法可以做到。同样仅供参考,您在某些课程之前缺少.。无论哪种方式,其中一些看起来可能太模糊,即(.btn可能适用于多个元素,但这是一个假设)

如果您更改此内容,您的前几次尝试可能有效:$('.nav-control pull-right a').click();到此$('.nav-control.pull-right > li > a').click();

或者,您可以尝试下面的一些也应该有效:

let el = $('[ng-click="loginSignUp()"]');
let el = element(by.cssContainingText('a', 'Login/Signup'));
let el = $('a.btn.btn-theme');

答案 1 :(得分:0)

您可以尝试使用javascript executor点击

var elm = element(by.linkText('Login/Signup'));
browser.executeScript("arguments[0].click();", elm.getWebElement());