Following sibling doesn't work on google search - xpath

I am trying to write my own Xpath using following sibling concept on Google Search page and trying to select "Google Search" button on that but when I check my Xpath in Firefox console it isn't able to identify the element.
I tried by removing following sibling tag and it worked but it doesn't work when I use following-sibling tag.
This works -
$x("//div[#id='searchform']/form/div[2]/div/div[3]/center/input[1]")
This is what I am using in console.
This doesn't work -
$x("//div[#id='searchform']/form/div[2]/div/following-sibling::div[2]/center/input[1]")
I am expecting it to highlight "Google Search" button but it isn't recognizing that.

"x/following-sibling::y" looks for a sibling of type "y" of type "x" element.
/div/div[3]/center looks for third div child of first div element,
but /div/following-sibling::div[2]/center looks for second div sibling of first div element and not child of it as in first case, which fails.
So in your case,
$x("//div[#id='searchform']/form/div[2]/div/div/following-sibling::div[2]/center/input[1]")
should work for you.

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.

How xpath works for tags in tags

I am trying to find out the xpath for first name of the facebook page and I have ended it with the following xpath: "**//div[1]/div[1]/div[1]/div[1]/input[#class='inputtext _58mg _5dba _2ph-']**" which is correct. My question is that, there are total 9 div tags on the page but I got it with the fourth div, I am not getting the reason how it's finding it in fourth div?
Page is Facebook home Page and element to find with xpath is Fist name input box
Please help me to understand how it's finding the element using above xpath
I know there are other ways to find xpath but I want to know the reason how it's finding it
I hope I am providing the complete information for the asked question if not let me know
Well it's because your xpath starts with a //. In literal english, it says find a DIV whose child is a DIV whose child is a DIV whose child is a DIV whose child is your INPUT. In your case, it does find a DIV which has INPUT as described by your xpath.
If you replace that // to single /, it will find the first DIV and then will try finding your input. Which it won't be able to find since .. like you said there are 9 DIVs.
Hope that paints a picture. Let me know if you need more explanation.

Watir how to click on nested element

I'm trying to click on "Mr" from the drop down list I've tried a combination of things but non of them seem to work.
I've even tried xpath which is usually reliable but for this case its failing.
$browser.element(:xpath, "/html/body/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div/div/div/div/ul/li[2]/a").click
The XPath suggested by Saurabh Gaur, can be written in a more readable Watir-like fashion using:
$browser.ul(class: 'dropdown-menu').link(text: 'Mr').click
Note that this assumes that there is only one ul element with class dropdown-menu. If there are multiple, you will need to scope the search to the specific dropdown using an element that likely exists higher in the DOM.
However, given there is likely only one link with text "Mr", you can probably get away with simply:
$browser.link(text: 'Mr').click
Given the link is a dialog that switches from hidden to visible, you may need to also wait:
$browser.link(text: 'Mr').when_present.click
Your xPath is positional which depends on element position.. it will not work if elements are change their position means adding some elements after some action on the page.
After seeing your attached image I have generated following xPath as below :-
//ul[contains(#class, 'dropdown-menu')]/descendant::span[contains(.,'Mr')]/parent::a
Try with this xPath.. May be it will work...:)

XPath Next Page navigation

I'm using Chrome Data Miner, and so far, failing to extract the data from my query: http://www.allinlondon.co.uk/restaurants.php?type=name&rest=gluten+free
How to code the Next Element Xpath for this website? I tried all the possible web sources, nothing worked.
Thanks in advance!
You could look for a tags (//a) whose descendant::text() starts with "Next" and then get the href attribute of that a element.
% xpquery -p HTML '//a[starts-with(descendant::text(), "Next")]/#href' 'http://www.allinlondon.co.uk/restaurants.php?type=name&rest=gluten+free'
href="http://www.allinlondon.co.uk/restaurants.php?type=name&tube=0&rest=glutenfree&region=0&cuisine=0&start=30&ordering=&expand="

XPath Expression

I am new to XPath. I have a html source of the webpage
http://london.craigslist.co.uk/com/1233708939.html
Now I want to extract the following data from the above page
Full Date
Email - just below the date
I also want to find the existence of the button "Reply to this post" on the page
http://sfbay.craigslist.org/sfc/w4w/1391399758.html
Can anyone help me in writing the three XPath expressions for the above three data.
You don't need to write these yourself, or even figure them out yourself. If you use the Firebug plugin, go to the page, right click on the elements you want, click 'Inspect element' and Firebug will popup the HTML in a viewer at the bottom of your browser. Right click on the desired element in the HTML viewer and click on 'Copy XPath'.
That said, the XPath expression you're looking for (for #3) is:
/html/body/div[4]/form/button
...obtained via the method described above.
I noticed that the DTD is HTML 4/01 Transitional and not XHTML for the first link, so there's no guarantee that this is a valid XML document, and it may not be loaded correctly by an XML parser. In fact, I see several tags that aren't properly closed (i.e. <hr>, etc)
I don't know the first one off hand, and the third one was just answered by Alex, but the second one is /html/body/a[0].
As of your first page it's just impossible to do because this is not the way xpath works. In order for an xpath expression to select something that "something" must be a node (ie an element)
The second page is fairly easy, but you need an "id" attribute in order to do that (or anything that can make sure your button is unique). For example if you are sure the text "Reply to this post" correctly identify the button just do it with
//button["Reply to this post"]

Resources