Protractor find an element by tag inside a div

2019-03-18 13:09发布

I've got a div in an HTML Page of which the name is always known and inside this div there is an href, the details of which are not known. It could be the direct child of the parent or it could be a further grandchild. Looks something like this:

<div class="divName">
    ...
        <a href="some url">some text</a>
    ...
</div>

I know that there will only be one link within this div, so I want to find the one link and click it.

I've tried the following but it doesn't seem to be working:

    element(by.classname('divName')).find('a').click();

Any ideas?

4条回答
够拽才男人
2楼-- · 2019-03-18 13:36
element(by.css('.divName a')).click();

Or the shorter notation:

$('.divName a').click();
查看更多
女痞
3楼-- · 2019-03-18 13:45

One line answer

try with element('.divName a').click();

查看更多
贪生不怕死
4楼-- · 2019-03-18 13:46

it seems sytax error in your code

use this

element(by.className('divName')).find('a').click();
查看更多
你好瞎i
5楼-- · 2019-03-18 13:57

figured out a solution:

ptor.findElement(protractor.By.className('clsName'))
    .findElements(protractor.By.tagName('a'))
    .then(function(links){
        links[0].click();
        //place expects here, otherwise it will run async and your expects will be hit 
        //before the lookup
});

This seems to work pretty well for my purposes

查看更多
登录 后发表回答