Finding an element by AccessibilityId and index - xcode

I have multiple buttons with the same accessibilityID. I want to get the 3rd one.
Getting the first one is easy with
let severityItems = app.buttons["MyListItems"].firstMatch
But if I want the 3rd one and do it this way
let severityItems = app.buttons["MyListItems"].element(boundBy: 2)
I get an error: Value of type 'XCUIElement' has no member 'element'
Strange. I seems to get an array. If I po it in the debugger it shows me the elements correctly. How can I get the 3rd element?

app.buttons["MyListItems"] returns an XCUIElementQuery, not an array of elements. If you look at the documentation for that you'll see a property called allElementsBoundByIndex, which returns an array of elements.

Related

How to select repeated element with Capybara

I know how to get the first element, but how can I get the second or third element found with that class x ?
Only way is by choosing from an Array like below? If so, starts at zero or one ?
find(".element")[1]
you can do
find(".element:nth-of-type(1)")
find(".element:nth-of-type(2)")
find(".element:nth-of-type(3)")
or
all(".element")[0]
all(".element")[1]
all(".element")[3]

Selenium find element which have no attributes but have parents with same attributes

I am trying to find XPath of an element which has no attribute. It can only be identified by its parent's attribute. However, the parent also does not have unique attribute.
Eg: //*[#id="btn"][1]/ul/li[2]/a/span
Here there are 2 elements with id=btn. How do i get the 2nd element. The above syntax gives me 1st element.. However if i use:
//*[#id="btn"][2]/ul/li[2]/a/span
I get an error message
"The xpath expression '//*[#id="btn"][2]/ul/li[2]/a/span' cannot be evaluated or does not result in a WebElement "
Try this, you select those two first, then use brackets around and index them.
(//*[#id="btn"]/ul/li[2]/a/span)[2]
By the way, it's not a good practice to have multiple elements sharing same ids, if you are the developer, may consider change them.

The "getnodevalue" of HTMLUNIT not working on domattr

every time i want to get the Value of my DomAttr i get an TypeError:
My Code:
Wanted = page.getByXPath("//span[contains(.,'Some')]/parent::a/#href");
return this
[DomAttr[name=href value=URLSTRING]]
Now i want to geht the value (=URLSTRING) with Wanted.getNodeName();
but every Time i get the Error
Cannot find function getNodeValue in object [DomAttr[name=href value=
same when i use getValue
please help me
There are some things that make no sense in the code (particularly, because it is not complete). However, I think I can guess what the issue is.
getByXPath is actually returning a List (funny thing you missed the part of the code in which you specify it as a list and replaced it with a Wanted).
Note you should probably also have type warnings in the code too.
Now, you can see that the returned value is in square brackets. That means it is a List (confirming first assumption).
Finally, although you happened to miss that part of the code too, I guess you are directly applying the getValue to the list instead of the DomAttr elements in the list.
How to solve it: If you need more than 1 result iterate over the elements of the list (that Wanted word over there). If you need 1 result then user the getFirstByXPath method.
Were my guesses right?

Prototype $$ returns array, should return one element like $

When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?
Changing the structure of the source is not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.
You can also do
$$('.foo').first()
It looks cleaner than $$('.foo')[0] for my taste :)
It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.
$$('.foo')[0]

Linq - OfType<> not working as expected

I have the following code which detects all the elements in a Silverlight application beneath a certain point
then filters them to be only those of a particular type - CardButton
IEnumerable<UIElement> elementsBeneathCursor =
VisualTreeHelper.FindElementsInHostCoordinates(new Point(xPosn, yPosn), Application.Current.RootVisual);
IEnumerable<CardButton> cardsBeneathCursor = elementsBeneathCursor.OfType<CardButton>();
Even though when I inspect elementsBeneathCursor in the debugger, I can see there are 2 elements of type CardButton
Yet when I apply the OfType<> filter the resultant list is null
what's going wrong?
The resulting list won't actually be null... but the sequence will be empty, if neither of those elements is actually a CardButton. Note that OfType doesn't perform any custom conversions, so if you were expecting those to happen, that may explain it.
Try going through the unfiltered list and printing out the result of calling GetType on each element to see what it really is.

Resources