I am having three divs each with class myDiv(just for example). And each of div has unordered list with list items inside it.
So i can write down xpath as
By.xpath("//div[#class='myDiv']/ul/li")
I want the first myDiv only.
But this will give results of all three divs. How to get only first div contents. Please help to modify this xpath.
As discussed with the OP, following are potential xpaths to go with.
//li[contains(#id,'100_deal')]
(//div[#class='gbwshoveler-content'])[position()=1]
//div [#id="deals-onethirtyfive-hero10903707629515"]//ul/li
(//div[#class='gbwshoveler-content'])[1]
you can get first div using
By.xpath("//div[#class='myDiv'][1]/ul/li")
Related
I'm trying to get an xpath so I can click a link as per href below:
<div id="viewIFL" style="">
<div class="moneycentrallink">
Track your cash in one place with
Money Central
</div>
</div>
When I use the below in ChroPath:
//a[contains(text(),'Money Central')]
It returns 2 elements matching for xpath="1" and xpath="2".
I then tried:
//a[contains(text(),'Money Central') and #xpath='2']
and at first it resolved to just 1 element found but when I tried searching again it returned 0 elements found. Also this does not work via Selenium either (returns unable to find element).
Any ideas what's going on and how I can find the unique xpath to clickable element? Thanks
Don't use xpath attribute in your xpath as ChroPath adds the xpath attribute in element to tell the user what is matching occurrence of that element. For example- If ChroPath added xpath=5 i.e. this element is the 5th for the corresponding xpath.
For your scenario, please inspect the element and see what ChroPath gives the relative xpath.
Also you can try //div[contains(text(),'Track your cash')]//a[contains(text(),'Money Central')]
Your problem is badly formulated.
There is always a unique path to an element of the form *[1]/*[4]/*[1]/*[2]. The problem is that this path isn't very useful because it only works if you know exactly what is in the document, and if you knew exactly what was in the document, you wouldn't need XPath to find it.
So you're actually looking for an XPath that will work on a set of possible documents in which some parts are known (fixed) and others are unknown (variable). To find an XPath that works on every document in that set, you need to define what is known and what is unknown. Looking at one sample document isn't going to tell you that.
I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!
I need to get the value 107801307 that is inside a specific Div but there are several other Divs in the path before getting into that DIV I need. Can anyone help?
Below is the image with the information that I need to extract from the DIV.
As per the HTML you have provided, to extract the text 107801307 you can use the following solution:
Java:
String myText = driver.findElement(By.xpath("//b[#class='label_tratamento'][contains(.,'Ban Claro')]//following::span[1]").getAttribute("innerHTML");
Python:
myText = driver.find_element_by_xpath("//b[#class='label_tratamento'][contains(.,'Ban Claro')]//following::span[1]").get_attribute("innerHTML")
Research xpath locators to find the specific element you want.
Assuming you were using Java, the code would be:
webdriver.findElement(By.xpath("//b[text()='Ban Claro']/following::span").getText();
or
webdriver.findElement(By.xpath("//b[#class='label_tratamento']/following::span").getText();
Use:
driver.findElement(By.xpath(("XPATH OF DIV HERE")[Index of the div where span is. Example: 4)/span)).getText();
Trying to find link element of "a href". Snippet code:
<div id="contact-link">
Contact us
</div>
I managed doing it by:
Driver.FindElement(By.XPath("//*[#title='Contact Us']")).Click();
2.Driver.FindElement(By.XPath("//a[#href='http://automationpractice.com/index.php?controller=contact']")).Click();
3.Driver.FindElement(By.XPath("//*[text()='Contact us']")).Click();
Could someone tell me how can I get by firstly getting parent div and then find what's inside that div (by going from the top to the bottom)
So basically, with xpath, you are looking to replicate the HTML structure. What you need is:
//div[#id='contact-link']/a
This is going to return the a href under the div. Assuming its just 1, thats the way to go. If you want to go a little further, try:
//div[#id='contact-link']/a[#title='Contact Us']
Although you have already accepted the answer , I would like to highlight some point about Xpath and cssSelector. You should always pick cssSelector over Xpath :
Here is cssSelector for your requirement:
div[id='contact-link']>a
Code :
Driver.FindElement(By.CssSelector("div[id='contact-link']>a")).Click();
For more about cssSelector : https://www.w3schools.com/cssref/css_selectors.asp
For Difference between Xpath and cssSelector, you can read it from this SO post: Diff between Xpath and cssSelector
I have this code for xpath and html:
<a class="WatchButton inicon" rel="nofollow" data-productid="111124">
xpath=/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a
How can I get the data-productid value?
Just add #data-productid to the xpath expression:
/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a/#data-productid
Note that the xpath expression you have is very fragile since it depends on a bunch of elements and their relevant positions. Try to rely on the element's attributes or one of it's containers - look for id and class attributes. For example:
//a[contains(#class, "WatchButton")]/#data-productid
This gets the first link anywhere on a page that contains WatchButton class and retrieves it's data-productid attribute value.
* Sharing the link to the web page or showing the complete HTML could help to provide you with a more reliable xpath expression.