How to solve the xpath issue of appium in marshmallow devices? - xpath

I am trying to automate an app . I have found few of the elements using xpath. In marshmallow devices, the elements are not able to find using xpath which is working fine in kitkat/lollipop devices. For example in KitKat/lollipop the xpath contains view; the same xpath in marshmallow instead of view it is taking as Viewgroup. How to provide the same xpath for all the version?
The following are the xpath of the same element in kitkat and marshmallow :
In kitkat: xPath is //android.widget.LinearLayout[1]/android.widget.FrameLayout[‌​1]/android.widget.Fr‌​ameLayout[1]/android‌​.view.View[1]/androi‌​d.view.View[1]/andro‌​id.view.View[1]/andr‌​oid.view.View[1]/and‌​roid.view.View[1]/an‌​droid.support.v4.wid‌​get.DrawerLayout[1]/‌​android.view.View[1]‌​/android.view.View[2‌​]/android.widget.Scr‌​ollView[1]/android.v‌​iew.View[1]/android.‌​widget.ScrollView[1]‌​/android.view.View[1‌​]/android.view.View[‌​1]
In Marshmallow: (for same element xpath is as below) //android.widget.LinearLayout[1]/android.widget.FrameLayout[‌​1]/android.widget.Fr‌​ameLayout[1]/android‌​.view.ViewGroup[1]/a‌​ndroid.view.ViewGrou‌​p[1]/android.view.Vi‌​ewGroup[1]/android.v‌​iew.ViewGroup[1]/and‌​roid.view.ViewGroup[‌​1]/android.support.v‌​4.widget.DrawerLayou‌​t[1]/android.view.Vi‌​ewGroup[1]/android.v‌​iew.ViewGroup[2]/and‌​roid.widget.ScrollVi‌​ew[1]/android.view.V‌​iewGroup[1]/android.‌​widget.ScrollView[1]‌​/android.view.ViewGr‌​oup[1]/android.view.‌​ViewGroup[1]

If you use RELATIVE XPATH your xpath will work for all OS versions.
You are using Absolute xpath which is very bad practice. It is always recommended to use Relative xpath.
You can read more about relative and absolute xpaths here

Related

Appium. Usage of Xpath axes in locator. Find sibling/parent/child/etc elements in context of curtrent element

Is it possible to find sibling/parent/child/etc mobile elements in context of existing mobile element.
For example I have basic element:
MobileElement mobileElement = driver.findElement(By.xpath("//any/xpath/locator"));
Then I need to find following sibling element.
In Selenium I could use similar code:
MobileElement nextMobileElement = mobileElement.findElement("following-sibling::nextelement['any conditions']");
But in appium I getting NoSuchElementException.
What is the proper syntax for usage of xpath axes in Appium to locate elements in defined above way?
In general if you want to find element under a parent element using xpath, add a . to the xpath to say that the child element xpath to be searched under the given parent element. So in your case you can try something like :
mobileElement.findElement(By.xpath(".(xpath of the child element)")
For example mobileElement.findElement(By.xpath(".//div[#class='xyz']")
Solution found in next way:
When appium is unable to select element by its name, i.e. there an element <android.widget.ViewAnimator ....>, then you can view it's "class" attribute, and it will contain an identical text: class="android.widget.ViewAnimator". So you can build your Xpath complicated locators based on this attribute. In case of Xpath axes it will look like this: //some/xpath/preceding-sibling::*[#class='android.widget.ViewAnimator'].
Appium supports XPath as WebDriver does. Make sure your XPath is correct, you can test it in appium-desktop. Following-sibling was working for me without any issues.
When Appium is not able to process XPath expression, you should get the related exception.

Locating WebElements using XPATH (NoSuchElementException)

I am having problems with locating elements using xpath while trying to write automated webUI tests with Arquillian Drone + Graphene.
To figure things out I tried to locate the search-button on the google homepage. Even that I am not getting done. Neither with an absolute or a relative xpath.
However, I am able to locate elements using IDs or when the xpath string has an ID in it. But only when the ID is a real ID and is not generated. For example on google homepage: The google-logo has a real ID "hplogo". I can locate this element by using directly the ID or the ID within the xpath-expression.
Why is locating the google logo using the ID "hplogo" possible but it fails while using the absolute xpath "/html/body/div[1]/div[5]/span/center/div[1]/div/div"?
I am really confused. What am I doing wrong? Any help is appreciated!
EDIT:
WebElement e = browser.findElement(By.xpath("/html/body/div[1]/div[5]/span/center/div[1]/div/div"));
is causing a NoSuchElementException.
Your expression works on
Firefox, but on webkit-based browser (e.g., chrome) the rendered DOM is a bit different. Maybe it depends on localization (google.co.uk for me). If I force on google.com the image logo for me is:
/html/body/div/div[5]/span/center/div[1]/img on firefox 37 and /html/body/div/div[6]/span/center/div[1]/img on Chome 42.
EDIT:
After discussing in chat, we figure out that HTMLUNIT is indeed creating a DOM that is different from the one real browsers render. Suggested to migrate to FirefoxDriver

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath:
"//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td"
by using this xpath, i am capturing the error message displayed on my application like "Please check your Password".
Now how can I write it in Webdriver. I have different ways but, not worked out.
"String msg= driver.findElement(By.xpath("//*[#td='error2']")).toString();" - This is what i did in Webdriver.
Please help me out on this...
The XPath hadn't change from Selenium RC to WebDriver.
If you were able to use the some XPath-expression before it should work in WebDriver too (in 99% of cases as usual).
The code below should work but it's pretty hard to answer without seeing your HTML.
String msg= driver.findElement(By.xpath("//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td")).getText();
//*[#td='error2'] - this expression looks bad as it means "any element with attribute named "td" and value "error2". I suppose that you don't have 'td' attributes in your HTML.
And to the end. I wouldn't recommend to use such long XPath expressions as in your example. It might be broken due to any minor change in layout. It's better to use some specific attributes rather than long hierarchy.
A good way to start would be to start with the basics : http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
What findElement would do is just locate the element that you want to interact with. After you locate the element you need to perform the action that you want to do with the element. In your case, you want the text inside the element. So you can look at appropriate functions of the WebElement interface and use the appropriate method, in your case, getText.

xpath for a google search url issue no content

http://www.google.com/search?q=youtube
Trying to get the url part www.youtube.com/ of the search
using this xpath but there is no output from it.
.//*[#id='rso']/li[1]/div/div[2]/div[1]/cite
i've also tried using the css path same issue
CSS: div[aria-label='Result details']+div>div cite.
Btw, your xpath works fine for me. If you use selenium to retrieve your text for example, you should write xpath=.// in this case, because it recognize selector as xpath by preceding // symbols. Also //*[#id='rso']/li[1]/div/div[2]/div[1]/cite//text() will return three textNodes www., youtube and .com/

Selenium WebDriver CSS equivalent to xpath not contains

I want to avoid using xpath in my java selenium code but cannot figure out what the css equivalent to the code below would be
("xpath", "//div[#class='error'][not(contains(#style,'display: none'))]");
Does anyone know if there is a CSS equivalent to xpath not contains
You can't easily match against a compound attribute (i.e., style=) in CSS. The not part is easy - CSS3 has a :not(...) selector. You're going to have to find something else to identify the elements you want to exclude, and then use :not(...) to exclude them,

Resources