I have a lot of difficult to find one element inside other.
enter image description here
Inside this, I need to compare if one of them, has an element with a specific text that I need, and click on it.
I already tried to use:
element.all(locator).filter(filterFn)
ERROR: - Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, #main > app-playbook-list-page > app-playbook-grid > div.d-flex.flex-row.flex-wrap)
element(locator).$(selector)
create a variable to save the all elements
Do someone have any idea to solve this?
$$('div.d-flex.flex-row.flex-wrap>div.flex-column');
Try the locator to get the array of elements.
Try this out this may help you
element.all(by.locator(element)).filter(function(el,index){
return el.isDisplayed();
}).first().click();
Related
I am trying to write a locator where the next text descendant is returned. I wont know the text. The following xpath works:
//*[#id='myChart']//label[contains(text(),"Show:")]/following::div[4]
but I dont like the div[4] as this could easily change. The element is the first div type descendant under show that contains text. Any suggestions?
A
Considering the following clauses:
the next text descendant
I wont know the text
div[4] as this could easily change
element is the first div type descendant
To locate the element a couple of effective approaches are as follows:
Using xpath:
//*[#id='myChart']//label[contains(., "Show")]//div[text()]
Using xpath with descendant:
//*[#id='myChart']//label[contains(., "Show")]//descendant::div[text()]
Using xpath with following:
//*[#id='myChart']//label[contains(., "Show")]//following::div[text()]
I think this will work for you:
//*[#id='myChart']//label[contains(text(),"Show:")]//div[text()]
To give more confident answer we need to see the actual page / XML.
In case the desired div is a direct child of the label containing the "Show:" the above expression can be presided to
//*[#id='myChart']//label[contains(text(),"Show:")]/div[text()]
How can I use xpath instead of id to find the element in DOM. I know that for id I can use: $("#id")[0].
I use $("#id")[0] inside Developer mode in browser in order to get the element by id to see what methods (like .getText(), innerHTML or others) am I able to use for the element. I want to know how to do this by XPATH
Thanks in advance
On Firefox console you can find (and explore) the element like this:
$x("//button[#id='myButton']")
But if you want to call a function on the element, you have to call it like this:
$x("//button[#id='myButton']")[0].click()
because there is always an array of elements returned (provided that the element is present and the first one in the array).
Thanks for the comments. I found out the answer. It can be done with:
$x("xpath")
So the other day I had my colleague review my code and he saw that I was using array[0], in Java terms this is basically getting the first element of the array. I did this several times for different purposes, all of which is to get the first element in an array/collection, for example list.get(0), to which he strongly disagreed with.
His argument was that somebody from non-programming background would have problem understanding it and using 0 in such cases is basically hard-coding, which is bad practice. I google-ed several times and all suggestions to getting the first element in an array or any collection is providing them the index, which is 0 in this case.
Could anyone provide me with a suggestion on getting the first element in a meaningful way?
Try using linkedlist's getfirst method to get the first element of the list.
If you were to use an ArrayList is backed by an array and hence its perfectly valid to use index as 0 to get first element.
Usually in protractor you can select singular element with:
element(protractor.By.css('#fdfdf'));
Occasionally you get something like this:
element(protractor.By.css('.dfdf'));
which potentially has more than one element. What's the correct way to select an index from a locator that locates multiple elements, and still contain the protractor's methods for sending Keys?
You can get an indexed element from an array returned with
// Get the 5th element matching the .dfdf css selector
element.all(by.css('.dfdf')).get(4).sendKeys('foo');
If you want to get the first element then
element.all(by.css('.dfdf')).first();
element.all(by.css('.dfdf')).get(0);
Try this one. It will work:
element.all(by.css('.dfdf')).get(4).getText();
I don't know why xpath is so much underestimated but you can solve thousands of problems with it, including this one
let elem = element(by.xpath('(//div//a)[3]'))
You can specify the number of element to use. Keep in mind the numbers start from 1, not 0 as usually in js
Grid had been added element by Row and Column,i want to add new element to grid by follow way:
grid.children[i] =element as UieElement;
It is invaild to me.have a error.
I am avoiding refresh in thread,so i have not clear Grid.Children.
try this :
grid.Children.RemoveAt(i);
grid.Children.Insert(i, element as UieElement);
(also, but I think it as a typo : Children should be capital C in grid.Children ...)
To add elements, this is how you would have to do it:
grid.Children.Add(element);
If you want to set the row/column, you can set the properties programmatically before adding it, like so:
element.SetValue(Grid.RowProperty, 1);
You can access an existing portion by index like in your example if you just need to access a specific child.