1

There are three blocks on the page, for example. example scructare Each block has a title and div.action with a button example The headings are different for everyone. I need to select a div with a Text heading and click a button

I find this element like this

return browser.element(by.css('.group-list-item:last-child')).element(by.css('.action'));

But the necessary block will not necessarily be the last, it can be 1 or the second. The title of the block does not change

show element show structure

2 Answers2

2

I am supposing the title of three blocks as Block1, Block2 and Block3

Now the button can be clicked for each block by passing the block title as the title of block is not changing and that can be passed run time. Assuming "group-list-item" is the class name say under tag div and also class name of button is "action" under tag button

clickButton(blockTitle: string): ElementFinder { return element(by.xpath("*//div[contains(@class,'group-list-item')]//div(text()='" + blockTitle+ "']//following-sibling::div")); }

V.Varshney
  • 151
  • 6
  • The problem is that the text itself lies in the div "title". At the moment, the element is simply not located. – tttyyyrrrryyy May 04 '20 at 05:20
  • Yes, that's why I posted the function in this way, whatever title you will pass, it will create xpath based on that, it will be more better if you can post the DOM of these page elements. – V.Varshney May 04 '20 at 17:57
  • The title itself lies in the div ".title". I laid out the structure in one of the links of my question. – tttyyyrrrryyy May 05 '20 at 05:20
0

Look at the answer I provided here.

I'm not a fan of using xpath because they are very fragile in dynamic or frequently updated applications.

Dynamically add an id to your elements. You can also use the following selector without invoking xpath:

get buttonByBlockTitle(searchTitle: string) {
    return elements(by.css('div.group-list-item').element(by.css(div.tittle).textContent(searchTitle).element(by.css('button.action');
}

public clickButton(searchTitle: string) {
    return this.buttonByBlockTitle(searchTitle).click();
}
ChadESmith42
  • 23
  • 1
  • 6