capture tooltip of a button using selenium - java

Am a Selenium beginner. have no idea as how to capture tooltip from the following html code.
this is the structure of the html:
<a id="aui_3_4_0_1_2236" title="Graceful shut down of the platform and power-off the hardware.">
<span id="aui_3_4_0_1_2235" class="aui-button">
<span id="aui_3_4_0_1_2234" class="aui-button-`enter code here`content">
<input id="_PlatformSummaryPortlet_WAR_CPFSPGPortlet10SNAPSHOT_INSTANCE_AEDwGJz6R6iD_soft" class="aui-button-input" type="button" value="Soft Shutdown" onclick="javascript:soft()" style="display: inline;"/>
</span>
</span>
</a>
and the title have the tooltip value.
I tried the following to get the tooltip:
WebElement Softshtdwn = driver.findElement(By.xpath(Object.SoftShutdownButton));
String tooltip = Softshtdwn.getAttribute("title");
String tooltip1 = Softshtdwn.getText();
String tooltip2 = Softshtdwn.getCssValue("title");
but for some reason, i get the null value in return.
Any help is appreciated.
note: cannot use by.ID as ID is dynamic.

Using below logic you can get tooltip
But, make sure that you are using proper and unique locator to get exactly required value.
String toolTip=driver.findElement(By.xpath("")).getAttribute("title");
If you are not able to construct proper xpath then post your html code here. I would help you.

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.

Unable to get text from div when text includes <span>

I am using Selenium with Java.I am unable to get a text from span tag, every time returns an empty string.
HTML code:
<div class="fade-repeat-animation alert ng-isolate-scope alert-error" ng-class="type && "alert-" + type" ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
<button class="close" ng-click="close()" type="button" ng-show="closeable">×</button>
<div ng-transclude="">
<span class="ng-scope ng-binding">This text</span>
</div>
</div>
This is the Selenium code:
String ipError = within(secs(10)).div(By.xpath("//div[#class='fade-repeat-animation alert ng-isolate-scope alert-error']/div/span")).getAttribute("innerHTML");
I tried use also getText() and getAttribute("textContent") but always returns empty String.
You have to try with getText() method, try this code
String Spantext=driver.findElement(By.xpath("//span[#class='ng-scope ng-binding']")).getText();
System.out.println(Spantext);
May be because of angularJS Webdriver is not able to read text from UI.
Try to use this code and let me know if it worked or not.
driver.findElement(By.cssSelector(".fade-repeat-animation.alert.ng-isolate-scope .alert-error")).getText();
As per the HTML you have provided to print the text This text you can use the following line of code :
System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='fade-repeat-animation alert ng-isolate-scope alert-error']//div/span[#class='ng-scope ng-binding']"))).getAttribute("innerHTML"));

Extract an image url from a LI in an UL

I am trying to extract the href value <img ng-src= from the following HTML.
Where everything after //images.mycar.com... is dynamic.
It is a <ul> with many <li>, it will be the first item in the list. I want to be able to capture it so I can construct a URL to click on the image.
Sorry if my post is confusing .. i will try and clarify .
In the example //images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg
Is the item i want to extract, so i can then click on the image.
<!-- Begin Results List -->
<ul id="resultsLists" class="o-media-list c-animate c-animate--show" ng-show="rc.results.vehicles.length" style="">
<li id="BJ11TFZ" class="c-animate c-animate--repeat u-pb ng-scope" ng-repeat="car in rc.results.vehicles track by car.registration" style="">
<div class="o-media c-card c-card--hover c-card__block u-p-0 u-shadowed u-shadowed--hover">
<div class="o-media__left o-grid__col-sm-5 o-grid__col-xs-12 u-p-0 u-no-float--sm">
<a ng-href="/used-car/FORD/FIESTA/BJ11TFZ" class="o-embed-responsive o-embed-responsive--16by9 o-media__object c-rollover" ng-click="rc.viewCar(car, $index)" href="/used-car/FORD/FIESTA/BJ11TFZ">
<img ng-src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg" alt="FORD FIESTA" class="o-embed-responsive__item c-rollover__image" cs-src-responsive="[ [ 'small', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ], [ 'retina', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ] ]" src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg">
</a>
</div>
If I understand your question right you are trying to extract this link below from the code above.
//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg
That's simple enough. A pure javascript solution to this would be:
var imgUrl = document.getElementById("resultsLists").getElementsByTagName('img')[0].getAttribute('ng-src');
The code starts by selecting the main UL of the document. Then it selects the image itself. Then it gets the "ng-src" attribute of the image and saves it to the variable imgUrl.
Let me know if this is what you were trying to do.
Okay bro, so in what's problem? If you need to use webdriver selenium and java 8
use next approach
driver.findElements(By.xpath("//ul[#id='resultsLists']/li//img")).stream()
.map(e -> e.getAttribute("ng-src"))
.collect(Collectors.toList())
You could get it like this below.
String href = driver.findElement(by.xpath("//ul/li/div/div/a")).getAttribute("href")

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.

Pressing toggle button on webpage in Java using Htmlunit

So I have basically been stuck on a particular piece of code for a few days now. I am trying to go to a webpage and click a specific toggle button using Htmlunit in Java.
The code I currently have for the java program:
WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("webpage URL");
page1.getElementById("additional_parameters_toggle").click();
The HTML code for the webpage toggle:
<div class="parameters clearfix">
<input type="checkbox" id="additional_parameters">
<label for="additional_parameters class="additional_parameters_toggle" data-name="big old ugly toggle">
<span class = "checkbox_outer">
<span class = "checkbox_inner"></span>
</span>
<span class="label_text">Show details / hourly data</span>
</label>
</div>
I figured that should work but I keep getting a NullPointerException. If there is anyone that could offer some insight or help in anyway, it would be greatly appreciated. Thank you.
As Tom has hinted, .getElementById() will not work since there is no id HTML attribute of that value.
You need to use:
page1.<HtmlElement>getFirstByXPath("//label[#class='additional_parameters_toggle']").click();
which means, find the first element by XPath: search for any label with a class attributes of "additional_parameters_toggle", then .click() it.
page1.getElementById("additional_parameters_toggle")
Looks like you don't have an element having this value as an Id. You do however have an element with an attribute class containing this value.

Categories