Find element by text inside another element using UISelector query - java

I have the following code snippet and the screenshot attached.
String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\"))" +
".scrollIntoView(new UiSelector().text(\"Test Group\"))";
driver.findElementByAndroidUIAutomator (query).click ();
What I want is to find an element with the text "Test Group" using UISelector, but inside the RecyclerView only (not searching the whole app source). What I get is the element inside search field instead (not in the RecyclerView).
Please advice. I know that I can get all searched elements using findElements(By.id("name")). But I want to use UI selector in this case.

With UiSelector you can use chaining:
String query = "new UiScrollable(resourseIdMatches(\".*recycler_view\")).scrollIntoView(resourseIdMatches(\".*recycler_view\")).childSelector(text(\"Text Group\")))";
In addition new UiSelector... part can be omitted. Appium does support this syntax.

Related

Appium NOT locating element when java variable is used in xpath

I'm trying to locate elements dynamically usign the xpath. However, when I use variable in the xpath, elements are NOT located. However, if I use hardcoded value, elements are located properly.
What am I missing here?
Below xpath locates the elements perfectly:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, 'hp')]"));
whereas, below xpath doesn't locate the elements:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
Please note that , there are multiple elements matching the above xpath.
I even tried below code but of no use:
driver.findElements(By.XPath(String.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
Any help would be appreciated.
Try do debug this issue as following:
Define the XPath string before calling driver.findElements method, format the string to have the proper value and then pass it into Selenium method, as following:
String xpathLocator = "//XCUIElementTypeStaticText[contains(#value, '%s')]";
xpathLocator = String.format(xpathLocator, device);
driver.findElements(By.xpath(xpathLocator));
As about your existing code.
Here driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
I can't see the formatting action.
And here driver.FindElements(By.XPath(string.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
it seems to be a wrong syntax.
It should be String.format while you wrote string.Format
Try trimming the spaces as:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '"+device+"')]"));
Or using String.format() as:
String device = "hp";
driver.findElements(By.xpath(String.format("//XCUIElementTypeStaticText[contains(#value, '%s')]", device)));
Note:
Instead of FindElements() it should be findElements()
Instead of String.Format() it should be String.format()
The issue was with the case mismatch in the value returned by variable. i.e; device variable was returning 'hP' instead of 'hp'.
Corrected the code and it works fine now.

Parsing xml with multi childs using jsoup

I have an xml file that looks as follows - link.
I would like to get the title from it.
In order to do so, I did the following:
Document bookDoc = Jsoup.connect( url ).parser( Parser.xmlParser() ).get();
Node node = bookDoc.childNode( 2 ).childNode( 3 ).childNode( 3 );
This returns me this:
Now I have 2 questions:
Isnt there any simpler way to get this title instead of using all of these childNodes? My worry is that in some result the title wont exactly be at childNode(3) and all my code wont work.
How do I eventually get this title? Im stuck at this point and cant get the string of the title.
Thank you
You can use selectors to access elements. Here you want to select by tag name. Two ways to get the element you want:
String title1 = bookDoc.select("record>display>title").text();
String title2 = bookDoc.selectFirst("record").selectFirst("display").selectFirst("title").text();
If you want to select more complicated things read:
https://jsoup.org/cookbook/extracting-data/dom-navigation
https://jsoup.org/cookbook/extracting-data/selector-syntax
But you probably won't need them for parsing this XML.

Java WebDriver Copy text Issue

Good Afternoon,
So I am trying to Copy some text from a field so I can paste it somewhere else in my test.
public static void validateTestCaseCreated(){
driver.findElement(By.xpath("//*[#id='mainForm:testTitle']")).click();
Action builder;
Actions copy = new Actions(driver);
copy.sendKeys(Keys.CONTROL + "a");
copy.sendKeys(Keys.CONTROL + "c");
builder = copy.build();
builder.perform();
The problem when it reaches line 6 it only sends c, it ignores the CONTROL. So my end result is not copying the text but highlighting the text then entering c.
You could just copy the value from the text field into a variable and store it for use later.
Pull it from the page using your code along with the get attribute method.
String valueInField = driver.findElement(By.xpath("//*[#id='mainForm:testTitle']")).getAttribute("value");
That will grab the text from the field and put it into the variable for later use.
I'm not sure if this is doing fully what you are trying to do, seeing as you are trying to do a crtl+c, but this method is how to grab text using webdriver.
If your field is an input element, maybe you can do something like this instead:
driver.findElement(By.xpath("//*[#id='mainForm:testTitle']")).click().get_attribute("value");

Jsoup url, get url by link name

I wanna get url by the link name.
download
ad
so what i want is the first url as the link name is download.
My question is how to get url by link name.
I know a complete solution is to get all elements and use if(a.text().contains(download) ). But I guess there is a simple way.
Thanks
Well, the best way would be to get all the < a>s, which contain hrefs, and get the hrefs attributes. Just like this:
Document doc = Jsoup.connect("whatever url").get();
Elements a = doc.select("a[href]");
String href;
for (Element elem : a) {
href = a.attr("href");
}
Now.. Which hrefs you wanna get is enterely up to you. But I think you'd have to use the
.contains("");
.endsWith("");
.startsWith("");
Oh, and maybe you could try using the getters from the doc variable.
.getElementsByAttributeValue("a[href]", "download");
Use a pseudo-selector. For example,
Document doc = Jsoup.connect(url).get();
Elements a = doc.select("a[href]:contains(download)");
Depending on what exactly you are trying to accomplish, you might want to use containsOwn to avoid searching within child elements, or use matches/matchesOwn if you want to use a regex to get elements that contain ONLY the text "download". That regex would be
^download$
See the Selector documentation.

Problems with element locating with XPath in Selenium-RC

I'm trying to perform very simple automated test. I created XPath selector in a FirePath, here it is:
//a[#href='http://i.yandex.ru/'][span[contains(.,'ledak.e.v#yandex.by')]]
But Selenium-RC can't locate this element. Code is:
final String StrEmailToTest = "ledak.e.v#yandex.by";
String linkEmailSelector = "//a[#href='http://i.yandex.ru/'][span[contains(.,'"+ StrEmailToTest + "')]]";
selenium.isElementPresent(linkEmailSelector);
and it returns "false"
Could you tell me, what am I doing wrong?
UPD. I've uploaded the *.maft - file here: http://depositfiles.com/files/lhcdh2wtl
Don't be afraid, there are some russian characters on the screen.
Shouldn't your XPath be:
"//a[#href='http://i.yandex.ru/']/span[contains(.,'"+ StrEmailToTest + "')]";
My guess is that selenium is looking for the element even before it's loaded. Is it a dynamically loaded/generated element? If so, use waitForElementPresent(). If not, try changing the method of element identification - use id or name and then try to execute it. To make sure your xpath is correct, in the selenium IDE/plugin for firefox, type the path of the element(issue some random command for command field) and click on "Find Element". If it finds, then selenium has no problem finding it, given that the page/element is loaded or generated. If not, you will have to ask Selenium to wait till the element is loaded.

Categories

Resources