how to find xpath of an element based on a number? - xpath

The issue I'm having is that I'm trying to find the xPath of the first element on a webpage that has more than 5 retweets. I'm trying to test in Selenium WebDriver using a testNG file. The page in question is the the following:
http://example.crowdynews.com/crowdynews/usa/politics/
I know I can just right click and find the Firepath of the element for it, but I would like to make the code dynamic considering that the news site is a dynamic as well.
If anyone can let me know how to do that or that could point me to a comprehensive site that could teach me how to do that, I would be very appreciative as I have yet to locate something that.

This is one possible XPath selector :
//b[#class='kudos-count' and number(.)>5]/ancestor::div[#class='item-wrap li-link']
brief explanation :
//b : find all b elements anywhere in the HTML document...
[#class='kudos-count' and number(.)>5] : ...where class attribute equals "kudos-count" and the content of the element, after converted to number, is greater than 5
/ancestor::div : from such b elements, return the ancestor div element...
[#class='item-wrap li-link'] : ...where class attribute equals "item-wrap li-link". The class indicates the div that contains the entire individual news

Related

How to find the exact element path without using xpath

I'm currently trying to locate this check box. I know I can use a xpath to locate it but I'm trying to see if there's a more efficient way of doing it. The problem I'm seeing is that there are multiple div class with the same name. I'm trying to find this specific one and isolate it. I'm trying to make my code more efficient if possible.
Xpath
/html/body/div/div/div/div[1]/cow-data/cat-panel/section/div[1]/div/div/md- checkbox[4]/div[1]
Element path:
<div class="cd-container" cd-gar-ripple="" cd-gar-ripple-checkbox=""><div class="cd-icon"></div></div>
Code I'm trying to use:
find('cd-container').click
The problem I'm seeing is that the div id 'cd-container' has multiple occurrences on the page and thus this doesn't work. I'm trying to see if I can find a more efficient way of doing this.
As per the HTML cd-container is the value of the class attribute but not id attribute. So your effective line of code will be:
find('.cd-container').click
If you want to find an element (AND THEN), return it's xpath. Use capybara.
This will allow you to locate using text / css selector. And then you can just return the path of the element.
i.e.
page.find('td', text: 'Column 1').path # Random td with text
page.find('#main').path # ID
page.all('div').select { |element| element.text == 'COoL dIv' }.first.path # First div that matches certain text
page.find('.form > div:nth-of-type(2)').path # Specific structured div
page.all('p div li:nth-child(3)').sample.path # Random li

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.

xpath queries to find closest element

I am working on project with java and selenium webdriver 2.44, we need the closest element of text. For example I have email text on page and i need the closest element that is text box. I have run the below given xpath query on facebook.com. However, it finds numbers of input type that is email and text. We do not need jQuery to run as we are using selenium webdriver.
.//*[contains(text(),'Email')]/following::input[#type='email' or #type='text']
Can anyone provide me xpath query to find closest element on page. Or a tutorial site where I can learn how to query.
Not a perfect solution, though in facebook scenario u can find first element as:
(.//*[contains(text(),'Email')]/following::input[#type='email' or #type='text'])[1]
Another thing to add if you are getting multiple elements for your query you can use:
List<WebElement> txtList = new ArrayList<WebElement>();
txtList = driver.findElements(By.xpath(".//*[contains(text(),'Email')]/following::input[#type='email' or #type='text']"));
and use txtList.get(0).sendKeys("abcd");

How to select all links on a page using XPath

I want to write a function that identifies all the links on a particular HTML page. My idea was to use XPath, by using a path such as //body//a[x] and incrementing x to go through the first, second, third link on the page.
Whilst trying this out in Chrome, I load up the page http://exoplanet.eu/ and in the Chrome Developer Tools JS console, I call $x("//body//a[1]"). I expect the very first link on the page, but this returns a list of multiple anchor elements. Calling $x("//body//a[2]") returns two anchor elements. Calling $x("//body//a[3]") returns nothing.
I was hoping that incrementing the [x] each time would give me each unique link one by one on the page, but they seem to be grouped. How can I rewrite this path so that I picks each anchor tag, one by one?
Your //body//a[1] should be (//body//a)[1] if you want to select the first link on the page. The former expression selects any element that is the first child of its parent element.
But it seems a very odd thing to do anyway. Why do you need the links one by one? Just select all of them, as a node-list or node-set, using //body//a, and then iterate over the set.
If you use the path //body/descendant::a[1], //body/descendant::a[2] and so on you can select all descendant a elements of the body element. Or with your attempt you need braces e.g. (//body//a)[1], (//body//a)[2] and so on.
Note however that inside the browser with Javascript there is a document.links collection in the object model so no XPath needed to access the links.

Resources