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

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"));

Related

Unable to click on an element using Selenium

<div id="_PHYLINSPortlet_WAR_PHYLINSPortlet_INSTANCE_o3P5_:form_PolicyContent_UI2:flatQuoteMenuBar:j_id7:VEHICLES" class="iceMnuBarItem portlet-menu-cascade-item">
<a class="iceLink" href="javascript:;" onclick="return Ice.Menu.cancelEvent(event);">
<span class="iceMnuBarItemLabel">Vehicles (1)</span>
</a>
</div>
I have to click on vehicles (1) menu
I tried lots of xpath selectors. gettext() is working but .click is not.
//driver.findElement(By.xpath("//*[#id='_PHYLINSPortlet_WAR_PHYLINSPortlet_INSTANCE_o3P5_:form_PolicyContent_UI2:flatQuoteMenuBar:j_id7:VEHICLES']/a/span")).click();
my 2nd xpath:
//driver.findElement(By.xpath("//span[(#class='iceMnuBarItemLabel') and contains (text(),'Vehicles')]")).click();
I guess issue is inside your html due to javascript was not defined properly.
Your html code see the below line of code.
<a class="iceLink" href="javascript:;" onclick="return Ice.Menu.cancelEvent(event);">
After modified your html see the below line of code.
<a class="iceLink" href="#" onclick="return Ice.Menu.cancelEvent(event);">
Now try to click on webelement using anyone of these below method.
Try this way to click vehicle element.
driver.findElement(By.xpath("//a/span[contains(text(), 'Vehicles (1)')]")).click();
OR
Click Vehicle element using Java-script executor.
WebElement vehicle = driver.findElement(By.xpath("//a/span[contains(text(), 'Vehicles (1)')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", vehicle);

Extract all visible text from html

I am trying to create a search function in google chrome. Given a string it will highlight all areas containing this string. I use java. I
To do this, first I need to extract all visible text. I have tried to analyze html pages in order to figure out how to extract only text.
For sections that looks like this, it seems
To do this, I planned on using jsoup. I am not sure how to extract text from sections that looks like this. (This is a youtube comment with a "read more" link and "show less" link).
From this section, I try to extract "Not gonna lie, dat dog is ADORABLE" and ("Les mer" or "Vis mindre" depending on which of them is visible).
<div class="comment-renderer-text" tabindex="0" role="article">
<div class="comment-renderer-text-content">Not gonna lie, dat dog is ADORABLE</div>
<div class="comment-text-toggle hid">
<div class="comment-text-toggle-link read-more">
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-link" type="button" onclick="return false;">
<span class="yt-uix-button-content">Les mer
</span>
</button>
</div>
<div class="comment-text-toggle-link show-less hid">
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-link" type="button" onclick="return false;">
<span class="yt-uix-button-content">Vis mindre
</span>
</button>
</div>
</div>
</div>
I am going to assume that the html code given is already in a document named doc.
String text = doc.select("div.comment-renderer-text-content").first().text();
The doc.select command gets Elements that contain that specified HTML query. Then I get the first one and convert it to text.
More can be read here: Jsoup Selector
Edit:
You can use this code to get visible text rather than per class:
String text = doc.body().text();

Element must be user-editable in order to clear it when try to enter date into date field

I'm trying to send date into date field with below statement. After running the script, getting below error message. Could you please help me out?
HTML:
<div class="forms-group-inline calendar-initialized" data-dateinputmanager-cutoffdate="2016-09-06" data-agent="DateInputManager">
<label class="col-6 forms-textinput date-input forms-compact ">
<span class="forms-labeltext">Leave</span>
<input type="text" placeholder="mm/dd/yy" value="2/24/16" name="ar.rt.leaveSlice.date"/>
<div class="forms-input-actions">
<div class="forms-input-actions-wrapper">
<a class="h1 forms-input-action" title="Leave" href="#">
<span class="offscreen">Leave</span>
<span class="i i-calendar"/>
Code trial:
driver.findElement(By.xpath("//a[#class='h1 forms-input-action' and #title='Leave']")).sendKeys("1/22/16");
I have tried the below code as well but getting same error:
WebElement BirthDate= driver.findElement(By.xpath("//a[#class='h1 forms-input-action' and #title='Leave']"));
BirthDate.clear(); BirthDate.sendKeys("1/20/16");
Exception:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element must be user-editable in order to clear it.
Command duration or timeout: 23.02 seconds
As per provided HTML code, the xpath you used //a[#class='h1 forms-input-action' and #title='Leave'] is for link (as tag is 'a'). So you can't do sendkeys as it is not input field.
<a class="h1 forms-input-action" title="Leave" href="#">
Please inspect input field with firebug/fire path or by selenium IDE and use that locator.
Thank You,
Murali
HTML <a> Tag
The <a> tag defines a hyperlink, which is used to link from one page to another. So you can't invoke sendKeys() on A tags.
Instead you need to invoke clear() and/or sendKeys() on the <input> tag as follows:
WebElement BirthDate = driver.findElement(By.xpath("//span[#class='forms-labeltext' and text()='Leave']//following::input[1]"));
BirthDate.clear();
BirthDate.sendKeys("1/22/16")

differentiate two html elements with same class

I have this html code below and I want to differentiate between these two PagePostsSectionPagelet as I only want to find web elements from the first PagePostsSectionPagelet. Is there any way I can do it without using <div id="PagePostsSectionPagelet-183102686112-0" as the value will not always be the same?
<div id="PagePostsSectionPagelet-183102686112-0" data-referrer="PagePostsSectionPagelet-183102686112-0">
<div class="_1k4h _5ay5">
<div class="_5sem">
</div>
</div>
<div id="PagePostsSectionPagelet-183102686112-1" class="" data-referrer="PagePostsSectionPagelet-183102686112-1" style="">
<div class="_1k4h _5ay5">
<div class="_5dro _5drq">
<div class="clearfix">
<span class="_5em9 lfloat _ohe _50f4 _50f7">Earlier in 2015</span>
<div id="u_jsonp_3_4e" class="_6a uiPopover rfloat _ohf">
</div>
</div>
<div id="u_jsonp_3_4j" class="_5sem">
<div id="u_jsonp_3_4g" class="_5t6j">
<div class="_1k4h _5ay5">
<div class="_5sem">
</div>
</div>
Tried using //div[#class='_1k4h _5ay5']//div[#class ='_5sem'] but it will return both.
Using //div[#class='_5dro _5drq']//span[contains(#class,'_5em9 lfloat _ohe _50f4 _50f7') and contains(text(), '')] will help me find the second PagePostsSectionPagelet instead.
you need to use the following xpath:
//div[contains(#class,'_1k4h') and contains(#class,'_5ay5')]
as selenium doesn't work properly with search of several classes in one attribute.
I mean By.Class("_1k4h _5ay5") will found nothing in any case and By.Xpath("//div[#class='_1k4h _5ay5']") can also found nothing in case of class will be "_5ay5 _1k4h" or " _5ay5 _1k4h".(as they possibly generated automatically, its may be have different position on page reload)
But for the best result by performance and by correctness I think will be the following xpath:
".//div[contains(#id, 'PagePostsSectionPagelet')][1]" -- for first div
".//div[contains(#id, 'PagePostsSectionPagelet')][2]" -- for second div
I see that dynamic in the div id is only the number so you can use something like:
WebElement element = driver.FindElements(By.XPath("//div[contains(.,'PagePostsSectionPagelet')])")[1];
This will take only the first web element.
Try using a css selector as below and refine further if required.
The code below returns a List of matching WebElements and then you grab the first one in the List.
List<WebElement> listOfElements = driver.findElements(By.cssSelector("div[data-referrer]"));
WebElement myElement = listOfElements.get(0);
Hint: use the Chrome console to test your css and xpath selectors directly. e.g. use
$$("div[data-referrer]") in the console to reveal what will get selected.

capture tooltip of a button using selenium

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.

Categories