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");
Related
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.
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.
I am trying to scrape full reviews from this webpage. (Full reviews - after clicking the 'Read More' button). This I am doing using RSelenium. I am able to select and extract text from the first <p> element, using the code
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#id][1]")
which is for less text review.
But not able to extract full text reviews using the code
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#id][2]")
or
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#itemprop = 'reviewBody']")
It shows blank list elements. I don't know what is wrong. Please help me..
Drop the double slash and try to use the explicit descendant axis:
/descendant::p[#id][2]
(see the note from W3C document on XPath I mentioned in this answer)
As you're dealing with a list, you should first find the list items, e.g. using CSS selector
div.srm
Based on these elements, you can then search on inside the list items, e.g. using CSS selector
p[itemprop='reviewBody']
Of course you can also do it in 1 single expression, but that is not quite as neat imho:
div.srm p[itemprop='reviewBody']
Or in XPath (which I wouldn't recommend):
//div[#class='srm']//p[#itemprop='reviewBody']
If neither of these work for you, then the problem must be somewhere else.
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
I'm trying to use Simple HTML DOM to find objects via XPath.
It's working pretty well but I can't seem to get the current element:
$object->find('.');
$object->find('..');
$object->find('//');
all return an empty array
$object->innertext
returns a normal table with HTML, so the object IS valid.
Simple HTML DOM doesn't recognize '.' for getting the current element,
in fact, it uses Regex to find elements using XPath.
In order to solve this problem I used DOMXPath instead of Simple HTML DOM,
which has a lot more options and functionality.