Having problems determining xpath - java

This one has me stumped. Usually I will use the Firebug feature to obtain the xpath for me but this time it doesn't seem to be working.
We have a US and UK version of our website. Firebug's xpath works on the US version but not on the UK version. If I grab the xpath using Firebug it's the exact same xpath for both sites but it works on the US version of the site and fails on the UK version of the site.
Here is the HTML:
<a class="ng-scope" ng-click="flightSearch.sort.$select(featuredItinerary.sortOrder)" tooltip="">
<h3 class="ng-binding">Fastest</h3>
<div class="row sort-block-row">
<div class="col-xs-12">
<h2 class="ng-binding">$283</h2>
</div>
I want to capture the value of the $283. This value will change constantly.
Here is what Firebug is telling me the xpath value is:
//html/body/div[2]/main/div/ui-view/ui-view/section/div/div[2]/div/ul/li[2]/a/div/div[1]/h2
Anyone have a better xpath value I can try and hope it works for both sites?

Following xpath should be able to help fetching the required value:
//a/h3[text()='Fastest']/descendant::h2

Try any of these below xpath
Assuming You are going to access the Fastest price.
Explanation: Start your xpath with <h3> tag using text method to match text as Fastest. Then move ahead with <h2> tag using following keyword. Because your <h2> tag is consider price.
//h3[contains(text(), 'Fastest')]/following::h2
OR
Explanation: Use class attribute of <div> tag. Then move ahead with <h2> tag. Because your tag is consider price.
//div[#class='col-xs-12']/h2

Related

Extract Text from Span Element via XPath [Selenium]

I have the following HTML:
<div class="a-row a-spacing-small a-size-small">
<div class="a-row">
<a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":"false","url":"/gp/customer-reviews/widgets/average-customer-review/popover/ref=wl_it_o_cm_cr_acr_img_hz?ie=UTF8&a=B05555JQP&contextId=wishi&link=1&seeall=1","name":"review-hist-pop.B075555RJQP","max-width":"700","position":"triggerBottom","data":{"itemId":"I2555555554GT","isGridViewInnerPopover":""},"header":"","cache":"true"}">
<i id="review_stars_I2J55555554GT" class="a-icon a-icon-star a-star-4-5">
<span class="a-icon-alt">4.5 out of 5 stars</span>
</i>
<i class="a-icon a-icon-popover"/>
</a>
<a class="a-link-normal g-visible-no-js" href="/product-reviews/B075555JQP/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=2K4U5555551D&coliid=I2J5555555T&showViewpoints=1">
<span class="a-letter-space"/>
<a id="review_count_I2J55555555GT" class="a-link-normal" href="/product-reviews/B05555555P/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=255555555D&coliid=I2555555GT&showViewpoints=1">(68)</a>
</div>
<div class="a-row">
<div class="a-row a-size-small itemAvailability">
<div class="a-row itemUsedAndNew">
</div>
I'm trying to extract the value 4.5 out of 5 stars via one of the following XPath:
.//*[contains(#id,'review_stars')]/span[#class='a-icon-alt']
.//*[contains(#id,'review_stars')]
However, everything that I've tried so far has failed (returns empty String)
The funny thing is that all of these XPaths actually work in Firebug so I'm not sure why it isn't working in my program (I suspect it has something to do with the fact that the rating isn't actually visible in browser unless you hover over a specific element but I'm not sure if/why/how this would cause the above mentioned problem and how to fix it)
Thanks!
You failed to include the image between the anchor and span. The span is inside the image, not a sibling of the anchor.
try:
.//*[contains(#id,'review_stars')]/i/span[#class='a-icon-alt']
I will attempt to answer my own question although I do not entirely understand why my previous code isn't working. If someone could provide me with an in depth explanation I will accept their answer as the final answer.
For now this is what works for me:
Instead of calling element.getText(); call element.getAttribute("innerHTML");
This returns the correct result but I would like to understand why getText() does not work in this case. Again, if someone could provide an XPath that works or could provide explanation to all this I will accept it as the final answer.
Thanks
To extract the value 4.5 out of 5 stars through XPath you can use :
//a[#class='a-link-normal a-declarative g-visible-js reviewStarsPopoverLink']/i[starts-with(#id,'review_stars_') and #class='a-icon a-icon-star a-star-4-5']/span[#class='a-icon-alt']
Update :
As you mentioned This does not work either. I just tried it. you must have missed out a part from the xpath which I have provided. My Answer was a proven one. See the snapshot below :
Note : Though your question was related to xpath you have pulled out your answer with respect to getText() method and getAttribute("innerHTML") method. How ever my Answer will be working with both getText() and getAttribute("innerHTML") method.

How do I click 1 of 2 links with same name, no id and same class. selenium java

I'm trying to get selenium to click the select button but I can't use by.linkText() because there are two buttons with the same name.
I'm using this xpath ".//*[contains(#id,'view-something_111111_2A22DF2_)']/div/a[text()='Select']"; to find the button but it can't find it.
I've also tried ".//*[contains(#id,'view-something_111111_2A22DF2_)']/div/a";.
I've looked over the Selenium documentation and can't seem to find a solution.
Here is the section of website code:
<div id="view-something_111111_2A22DF2_0" class="coverage-wrap collapse" aria-expanded="false" style="height: 30px;">...</div>
<div class="btn-raplace">
<a class="btn-beer" data-toggle="collapse" data-target="#view-effectData_111111_2A22DF2_0">Select</a>
for reference, the second Select button has this code:
<div id="view-something_111111_2A3B5DF2_0" class="coverage-wrap collapse" aria-expanded="false" style="height: 30px;">...</div>
<div class="btn-raplace">
<a class="btn-beer" data-toggle="collapse" data-target="#view-effectData_111111_2A3B5DF2_0">Select</a>
which is why I am using the id in my xpath.
Thanks.
You can try this XPATH :- //*[#class="btn-raplace"]/a[#class="btn-beer"][1] here [1] is postion of ur button. Which you want to click
I can see two mistakes in the Xpath you are using.
First Mistake:
.//*[contains(#id,'view-something_111111_2A22DF2_)'] is incorrect.You have placed the single quote at a wrong place. It should be
//div[contains(#id,'view-something_111111_2A22DF2')]
Second Mistake
The element div with the class="btn-raplace" is not the child of the above element. I can see in the HTML that the above element has the closing tags before this element.
Please replace your XPATH with:
//div[contains(#id,'view-something_111111_2A22DF2')]/following-sibling::div[1]/a
Here is the Answer to your Question:
Use this xpath:
//div[#class='btn-raplace']/a[#class='btn-beer']
Let me know if this Answers your Question.

How to identify a locator in below Html code and how to write a xpath for this in selenium?

How to identify a locator in below Html and how to write a xpath for
this in selenium
<div>
<div class="slds-icon-waffle" data-aura-rendered-by="231:0;p">
<div class="slds-r1" data-aura-rendered-by="232:0;p"></div>
<div class="slds-r2" data-aura-rendered-by="233:0;p"></div>
<div class="slds-r3" data-aura-rendered-by="234:0;p"></div>
<div class="slds-r4" data-aura-rendered-by="235:0;p"></div>
<div class="slds-r5" data-aura-rendered-by="236:0;p"></div>
<div class="slds-r6" data-aura-rendered-by="237:0;p"></div>
<div class="slds-r7" data-aura-rendered-by="238:0;p"></div>
<div class="slds-r8" data-aura-rendered-by="239:0;p"></div>
<div class="slds-r9" data-aura-rendered-by="240:0;p"></div>
</div>
You can generate xpath as below using div class :
//div[contains(#class,'slds-icon-waffle')]
Hope it will help you.
Here class names are looking unique. you can use the following xpath to identify the first elemen.
//div[#class="slds-icon-waffle"]
for the second element
//div[#class="slds-r1"] and so on.
If you want to locate all elements with single xpath, then use the following.
//div[starts-with(#class,"slds")]
Writing xpath is a pretty basic thing in selenium. I prefer you to learn how to write xpath first. This video might give you some idea.
http://learn-automation.com/how-to-write-dynamic-xpath-in-selenium/
Once you got some idea, try writing the xpath by yourself. If you got any issues, feel free to comment. Thanks.

Having trouble locating text within span tags

I'm specifically searching for "Leg 0" and the other text below it like BOS and MAD, etc. I've played around with xpath and I seem to find text above this section and text below it but I can never find the "Leg 0" text and that below no matter what I do. I've been trying all morning to figure this out and no dice.
Here is the HTML.
<div>
<span class="debug-section ng-binding">Leg 0</span>
<span class="debug-value ng-binding">BOS</span>
-
<span class="debug-value ng-binding">MAD</span>
<span class="debug-value ng-binding">BACAT</span>
/
<span class="debug-value ng-binding">BA</span>
<span class="debug-value ng-scope" ng-if="!leg.excludeDynamic">Dynamic</span>
</div>
Any help is appreciated here, I am beyond frustrated.
First of all, provide some info about your code and exception you get as #Anderrson suggested.
And second, you can't find element based on text within span tags (innerHTML) using cssSelector, you should use xpath in that case. Or some javascript or jquery, but that would probably be overkill in this case.
The answer my friend is blowing in the wind. Brought up Firefox. Enabled the Firebug plug-in. Click the inspection tool. It highlights the HTML line you click on on the web page. Right click. Choose the Xpath copy to clipboard option.
Walla.

cannot navigate to the target tag in DOM by using numbers in bracket, like div[4]

I am using Java and Selenium to write a test. I have this at DOM:
...
<div class='a'>
<div class='b'>
<div class='1'></div>
<div class='2'></div>
</div>
/div>
...
I am using this Xpath:
//div[#class='a']//div[2]
to get to <div class='1'></div>
but I am taken to:
<div class='2'></div>
in simple words please explain why and how to handle this situation. I do know I can use the class attribute to get to an element like :
//div[#class='1']
but I want to use numbers in bracket style like div[and-a-number-here]
Is there any way to get all divs under a tag and the select the one by the number?
In your xpath:
//div[#class='a']//div[2]
you are searching for any div which is second descendant of the div with a class=a. That's why it's returning you the 2nd div with class=2.
To get the desired element use xpath like this:
//div[#class='a']/div/div[1]

Categories