Selector to extract element value of type different tagName - automation

There is String on my web page which looks as "#Account9 Hey Dude". #Account9 is a link, 'Hey Dude' is span. Please help me to create Selector to extract this "#Account9 Hey Dude".
note: Can find something like selenium "normalize-space" method.
#Account9
Hey Dude

To create a selector that extracts the inner text of the provided markup:
<div class=“XYZ123>
<span class="r-18u37iz">
<a href="/Account9" dir="ltr" role="link" data-focusable="true" class=“ABC123”>#Account9 </a>
</span>
</div>
<span class=“ABCxyz123”> :Hey Dude</span
you need to find the first parent of these elements (div class=“XYZ123 and span class=“ABCxyz123”) , specify CSS selector for it and call innerText property.
const targetText = await Selector('<parent_of_these_elements>').innerText;

Related

Selenium getting href link returning null

Getting href link from following html is always giving me null. I have been trying multiple ways to get this I cannot use xpath since xpath changes for every page.
<div class="form-group" id="idfb">
<label class="control-label">PDF </label> <a type="button" value="Download" href="./decisiondecisionForm-pdfContainer-filePdfDownload&id=6303"><i class="fa fa-download fa-2x xh-highlight" aria-hidden="true"></i></a><br>
</div>
I am trying to get href in following way
val element = driver.findElement(By.cssSelector("*[id^='id']"))
val link = element.getAttribute("href")
Is issue in aria-hidden attribute ?
Actually you want get the href attribute from the <a> tag, and your selector is not referring to it, but to the div tag.
The div tag doesn't have the href attribute, so that's why your code returns null.
So, instead you can try with the following value: By.cssSelector("div[id^='id'] > a")
Can you try this: Assuming div class="form-group" id="idfb">
is parent tag.
val element = driver.findElement(By.cssSelector("*[id^='id' a]"))

How to get complete text inside page-footer from div

How to get complete text inside page-footer in the below case.
<div class="page-footer">
<span >Environment: </span>
<span > test</span>
<span >Version: </span>
<span > 0.1.56</span>
</div>
You can use this CSS_SELECTOR :
div.page-footer span
this will select all the span which are inside the div with class page-footer.
In case, you are using JAVA as a binding language :
String allText = driver.findElement(By.cssSelector("div.page-footer span")).getText();
Hope this helps.
You can use the below method to extract all the visible text
In Java,
getText() method will return the visible text of an element (i.e. not hidden by CSS)
string footerText=driver.findElement(By.className("page-footer")).getText();
In C#,
var footerText=_driver.FindElement(By.ClassName("page-footer")).Text;

No such element Exception. class name with double__

I am trying to get the text under the tag. That is Arduus, Gstaad, Switzerland. I tried with the classname and also with xpath.
driver.findElement(By.className("chalet-details__title"));
String chaletTitle = driver.findElement(By.xpath("//div/h1[#class='chalet-details_title']")).getText();
But its giving NoSuchElementException. The classname has double underscore(__) .Is it because of that not working? Can anyone help me with this?
<div class="col-md-12 col-sm-12 col-xs-12">
<h1 class="chalet-details__title">
<span class="chalet-details__title-property">Arduus</span>,
<a class="chalet-details__title-resort" href="/ski- resorts/switzerland/gstaad">Gstaad</a>,
<a class="chalet-details__title-country" href="/ski- resorts/switzerland">Switzerland</a>
</h1>
<div class="chalet-details__rating">
<div class="chalet-details__wrapper">
<span class="chalet-details__star" style="width: 108px;"></span>
<span class="chalet-details__mask"></span>
</div>
</div>
</div>
You can try something like:
driver.findElement(by.cssSelector("h1.chalet-details__title"))
I just tried this and it worked fine with the provided html, if this still fails for you, can you verify that there aren't any iframes on the page.
Both your locators are OK, but you might need to wait for your element to be present in DOM:
WebDriverWait wait= new WebDriverWait(driver,20 );
String chaletTitle = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("chalet-details__title"))).getText();
Another reason for NoSuchElementException: your element could be located inside an iframe, so you need to switch to that frame before handling target element:
driver.switchTo().frame("iframeNameOrId");
If it has no attributes as id or name:
WebElement someFrame = driver.findElement(By.xpath("//iframe[#someAttr='someValue']"));
driver.switchTo().frame(someFrame);
To get all the text under h1 tag find all elements using locator as follows:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *")); //it will find all elements immediately under the h1 tag
Now, iterate over the elements.
Complete code:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *"));
for(WebElement item : items){
System.out.println(item.getText());
}

Any suggesstions to locate the below elements in Selenium

Example HTML below. I want to locate the elements which contains the text Person Attributes and Age.
<div id="ext-gen210" class="x-tool x-tool-toggle"></div>
<span id="ext-gen214" class="x-panel-header-text">Person Attributes</span>
</div>
<div id="ext-comp-1071" class=" DDView ListView" style="height: auto;">
<p class="dragItem "><i class="icon-Gen"></i>Age</p>
</div>
Note: I am looking for a solution without using xpath or id or className as they might change with every new release of my website.
I tried to locate them using
'name' --> By.name("Person Attributes") and By.name("Age") but both failed.
By.name would check for the name attribute. What you need is to check the text of an element using By.xpath:
By.xpath('//div[span/text() = "Person Attributes"]')
Or, you can also check that an id element starts with ext-gen:
By.xpath('//div[starts-with(#id, "ext-gen")]')

Hot to find in dojo element when I know id of parent and I know type and style class of element which I looking fo

Hot to find in dojo element when I know id of parent and I know type and style class of element which I looking for ?
For example, I want find and change (span style=tabLabel) ALARMS into Mga alarma
<div dojoattachpoint="focusNode" role="tab" style="-moz-user-select: none;" id="tab_div_tablist_dijit_layout_ContentPane_1" tabindex="-1" title="" aria-selected="false">
<img dojoattachpoint="iconNode" class="dijitIcon dijitTabButtonIcon dijitNoIcon" alt="" src="dojoroot/dojo/resources/blank.gif">
<span class="tabLabel" dojoattachpoint="containerNode" style="-moz-user-select: none;">Alarms</span>
<span role="presentation" dojoattachevent="onclick: onClickCloseButton" dojoattachpoint="closeNode" class="dijitInline dijitTabCloseButton dijitTabCloseIcon" style="display: none;">
<span class="dijitTabCloseText" dojoattachpoint="closeText">[x]</span></span>
</div>
In this case it is pretty easy. If you look at the span element you refer to it has a dojoattachpoint attribute specified. That means that the node can be accessed from the widget directly with that name.
Now I assume that the widget is called "tab_div_tablist_dijit_layout_ContentPane_1" from the id in your code so to get the widget:
var widget = dijit.byId("tab_div_tablist_dijit_layout_ContentPane_1");
And the dojoattachpoint on the span has the value containerNode so:
widget.containerNode.innerHTML = "Mga alarma";
I think that should work.
If you're creating a custom widget template and wish to localize a string, there is a mechanism to do this. Simply use a substitution pattern like ${alarm} and define a javascript property on your widget with that name. That property can then be populated with a localization bundle using dojo.i18n. You can look at some of the dijits like dijit.Dialog.postMixInProperties to see how this is done.