Appium XPath Selector returns WebDriverException - xpath

I'm attempting to perform some automation of an Android app, using NGTest framework and UIAutomator.
The below is output from UI Automator - my XPath knowledge is not the hottest but I've attempted to select the parent of the Camera button using the following Java code:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id=com.instagram.android:id/tab_bar']"));
However this returns an exception:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
Could anyone guide me better on how to use XPath expressions to drill down into the tree from UIInspector? Should I be starting at the top and working my way down, or is it ok to try and grab children in this way?
I have also tried grabbing the camera icon directly using the bounds, but that didn't work either.
Attempted:
WebElement abc = driver.findElement(By.xpath ("//android.widget.ImageView[contains(#bounds,'[508,2119][571,2131]')']"));
abc.click();

I think you are missing a quote sign at the beginning of your resource-id value.
You are using:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id=com.instagram.android:id/tab_bar']"));
And it should be:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id= 'com.instagram.android:id/tab_bar']"));
This element explorer can really help you locating the elements

Related

Trying to Get child node(Switch ) from Parent(LinearLayout) via AndroidFindby(resourceid), Appium

(class=android.widget.LinearLayout,resource-id=settings_language_selection_toggle,index=1 )
(class=android.widget.RelativeLayout,index=0)
(class=android.widget.LinearLayout,resource-id=widget_frame, index=1)
(class=android.widget.Switch,resource-id=switchWidget, index=0)
/////////////
You can have a look at the hierarchical representation of the elements below
Hierarchy Image View:
***I have tried to reach the switch button by writing the below code on appium Android for java, but it did not work
#AndroidFindBy(xpath ="new UiSelector().resourceId(\"com.idscan.mjcs.sample:id/settings_language_selection_toggle\").instance(1).getChildById(new UiSelector().className(\"android.widget.Switch\")
You using UiSelector syntax in XPath strategy. That is why it isn't working. Try this one:
#AndroidFindBy(uiAutomator = "resourceId(\"settings_language_selection_toggle\").childSelector(className(\"android.widget.Switch\"))")
As you can see here, some boilerplate can be omitted, e.g. new UiSelector().resourceId(...) can be simplified to resourceId(...). One more thing: once you found the root element (LinearLayout with the given resourceId), you can find any child element in the hierarchy using .childSelector() method, nesting doesn't matter.

Error initializing driver method - undefined local variable

In my test automation project, I am using ruby with capybara, cucumber and selenium.
(I already have devkit installed and chrome is starting normally)
When looking for an element in the site to select it, I am using the method driver.findElement (By.xpath (.... etc"), but when executing cucumber, it is indicating the following error:
I already removed and reinstalled the selenium-webdriver gem but it did not resolve.
Can someone help me in resolving why WebDriver does not seem to be valid in this context?
code example
(finding button logout using tag img, because the element don't have name or id)
After('#logout') do
element = driver.findElement(By.xpath("//img[#src='/Portal/img/user.png']"));
element.click
end
Result cucumber execution
Feature: Login
Description feature
DevTools listening on ws://127.0.0.1:60121/devtools/browser/c0bacc6e-697a-4614-b82c-eb324d587df5
#logout
Scenario: Login_OK # features/support/login.feature:14
Given that i access the main portal page HRP # features/step_definitions/login_steps.rb:1
When do login using "abc123" and "abc123password" # features/step_definitions/login_steps.rb:5
Then system do login # features/step_definitions/login_steps.rb:10
And show message "Welcome to Portal." # features/step_definitions/login_steps.rb:14
undefined local variable or method `driver' for # (NameError)
./features/support/hooks.rb:4:in `After'
Failing Scenarios:
cucumber features/support/login.feature:14 # Scenario: Login_OK
1 scenario (1 failed)
4 steps (4 passed)
0m5.457s
If you're using Capybara with Cucumber in a standard setup then you shouldn't be really ever be calling the selenium webdriver instance directly - driver (except for in some very rare circumstances). Instead you should be using the Capybara methods. What is available in your After hook depends on exactly how where you've includeed what but one of these will probably be avaialble
After('#logout') do
element = page.find(:xpath, ".//img[#src='/Portal/img/user.png']"));
element.click
end
or
After('#logout') do
element = Capybara.current_session.findElement(:xpath, ".//img[#src='/Portal/img/user.png']");
element.click
end
Note: there is probably a better way to locate the logout button than an XPath on the contained images source, but without seeing the HTML of the button it's impossible to say for sure (for instance, Capybaras button finder will match against the alt attribute of an img element nested inside a <button> element)

Nightwatch.js - assert.containsText I'm looking for a string but got empty (null)

Here is a spinet of my code:
browser
.waitForElementPresent('(//*[#class="textsize-xlarge"])[1]',5000)
.assert.containsText('(//*[#class="textsize-xlarge"])[1]', 'TEST-69554083978')
I want to verify that an element has a string using assert.containsText().
So I'm trying to check if the class textsize-xlarge contains text
"TEST-69554083978"
Visually I can see that it does but nightwatch returns an error message saying expected
"TEST-69554083978" got ""
Can anyone please tell me what I'm doing wrong here and point me in the right directions?
browser.useXpath(); // xpath will be used as the locating strategy so
all the selectors you pass should be xpath selectors
browser.useCss(); // switches back to css selector locator should be
your xpath
let locator = "[#class='textsize-xlarge']//*[contains(text(),'TEST-69554083978')]";
// Please modify above locator as I am not aware of your DOM but I can help further if you need any help in Xpath
return browser
.useXpath()
.waitForElementPresent(locator)
.assert.elementPresent(locator);
you can learn about node assertions: Node Assertions

HP UFT - WebElement cannot be found even after using correct xpath

I am new to UFT. I have a long "complex" xpath that finds precisely one element in chrome browser developer tools. When I use the same xpath in uft, the tool complains that the xpath is not in the object repository. Why does this happen and how do I fix it ?
This is what the xpath looks like:
//div[#class='a b c']//div[#class='p-q r-s']//div[#class='m n']//button[contains(text(), 'yes')]
I have to use such complex xpath because there are no ID attributes in this part of the page or any other 1-2 attributes which can uniquely identify the element.
Please help.
EDIT: My Vbscript code looks similar to this code:
Dim aButtonLoc
aButtonLoc = "//div[#class='a-b c-d-e g']" & _
"//div[#class='p-q r-s-t']//div[#class='uv w-x']" & _
"//button[contains(text(), 'Yes')]"
Error message: The {full xpath here} object was not found in the Object Repository. Check the Object Repository to confirm that the object exists or to find the correct name for the object.
There is an error in my aButtonLoc locator. It should have xpath mentioned, like this: aButtonLoc = "xpath:=//d...etc."

Unable to locate table element generated via ajax using Webdriver

I am new to Webdriver, I can not let you know the site name and credentials as this is an ongoing project of my company.
I am getting stuck a place, in a page of that website, there is a table generated via ajax on runtime and all data inside it also generated on runtime, there is an element inside that table with xpath as html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a, but when I tried to find this element using webdriver, webdriver is unable to locate the element and I am getting an error message as
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a"}
Command duration or timeout: 50.10 seconds
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=14.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
My code for this is
Assert.assertEquals(FrameWorkBase.driver.findElement(By.xpath("html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a")).getText(),24514);
When I entered the above xpath in firebug, it is locating the element, means xpath is correct.
Please help me out.
Thanks
Couple of things :
1. Try having an explicit wait on your element, something to the effect..call this method for your elements.. read more
public WebElement waitForElementToBeVisible(WebDriver driver, By by){
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(by));
return element;
}
Your xpath html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a looks a bit brittle since it's absolute. Try finding a unique identifier (an id would be best) for one of the elements and then find your way to your element.
If the table is ajax-generated, perhaps you should implement some sort of waitForElement instead of a simple findElement? In my tests I have written a method that waits until jQuery is ready, and it works quite nice. I suspect the reason for your exception is that the element simply isn't present when you are trying to find it.
You could also try first to get only a higher-level element and then work your way deeper. That way you can get a clue what element is missing and see what kind of wait is needed.

Resources