I want to print some data using selenium, here is HTML:
<div class="details-row section box-margin-LRmd is-disabled">
<div class="inline-flex-group-v2">
<div class="item gutter-sm box-fixed col-encounter-type">
<label class="field-label">Encounter type</label>
<div data-element="encounter-details-encounter-type">
Office Visit
<!----> </div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding">
<label class="field-label">Note type</label>
<div data-element="encounter-details-encounter-type">Case Review</div>
</div>
<div class="item gutter-sm box-fixed col-date">
<label class="field-label">Date</label>
<div data-element="encounter-details-date" class="clearfix">
05/01/2018
</div>
</div>
<div class="item gutter-sm box-fixed col-age visible-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">FOCUS Pittsburgh Free Health Center</div>
</div>
<div class="item gutter-sm visible-xl">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
<div class="inline-flex-group-v2 hidden-xl box-padding-Tmd-v2 lower">
<div class="item gutter-lg box-fixed col-age hidden-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-lg box-fixed col-seen-by">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-lg box-fixed">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">Health Center</div>
</div>
<div class="item gutter-sm">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
</div>
I want to print encounter type, note type, date, age and seen by. So I use code :
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-encounter-type']")).getText());
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-encounter-type']")).getText());
System.out.println("DATE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-date']")).getText());
System.out.println("AGE OF ENCOUNTER: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-age']")).getText());
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-seen-by']")).getText());
And I got:
Encounter details
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Office Visit
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY:
So encounter type and note type have same attribute "data-element='encounter-details-encounter-type'", that's why I got same result for "encounter details" and "encounter type", but what I want to get is
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Case Review
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY: Kristina Abernathy
So how should I get correct note type and why can't I get "SEEN BY" content? Thanks!
From your html I can see that the Encounter Type and Note Type field is using the same element for reference. That's why you are getting same result for the different headings. While you are selecting the locators for identifying an element it is better to use unique values for that element otherwise these kind of issues will occur. Please check with the below code for Note Type,
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[label='Note type']//*[#data-element='encounter-details-encounter-type']")).getText());
I have checked the SEEN BY field, it is working fine.
But if you want to take the first SEEN BY record, then use the below code,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(#class,'extra-padding visible-xl')]//*[#data-element='encounter-details-seen-by']")).getText());
For the second SEEN BY,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(#class,'fixed col-seen-by')]//*[#data-element='encounter-details-seen-by']")).getText());
As per your requirement, you can use cssSelector for Note Type.
Code you can try out:
System.out.println("NOTE TYPE: " + driver.findElement(By.cssSelector("div[class$='col-extra-padding'] div")).getText());
If you still wanna go with Xpath, then you can try this code
Xpath :
//label[text()='Note type']/following-sibling::div
Code :
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//label[text()='Note type']/following-sibling::div")).getText());
There are two Seen By:
For first one you can write cssSelector as :
div[class*='col-extra-padding']>div[data-element$='-seen-by']
Code
System.out.println("First Seen By: " + driver.findElement(By.cssSelector("div[class*='col-extra-padding']>div[data-element$='-seen-by']")).getText());
Xpath would be:
//div[contains(#class,'col-extra-padding')]/child::div[#data-element='encounter-details-seen-by']
Code:
System.out.println("First Seen By: " + driver.findElement(By.xpath("//div[contains(#class,'col-extra-padding')]/child::div[#data-element='encounter-details-seen-by']")).getText());
For Second Seen By you can write cssSelector as :
cssSelector :
div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div
Code
System.out.println("Second Seen By: " + driver.findElement(By.cssSelector("div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div")).getText());
Xpath :
//div[contains(#class,'box-padding-Tmd')]/child::div[contains(#class,'col-seen-by')]/child::div
Code
System.out.println("Second Seen By: " + driver.findElement(By.xpath("//div[contains(#class,'box-padding-Tmd')]/child::div[contains(#class,'col-seen-by')]/child::div ")).getText());
To retrieve the Encounter Details you can create two Lists one for the Headings and the other for the Values and print them as follows :
List<WebElement> items = driver.findElements(By.xpath("//div[#class='inline-flex-group-v2']//div[contains(#class,'item') and contains(#class,'gutter-sm') and contains(#class,'box-fixed')]/label"));
List<WebElement> value = driver.findElements(By.xpath("//div[#class='inline-flex-group-v2']//div[contains(#class,'item') and contains(#class,'gutter-sm') and contains(#class,'box-fixed')]//div[starts-with(#data-element,'encounter-details-')]"));
for(int i=0; i<items.size(); i++)
System.out.println(items.get(i).getText() + " value is : " + value.get(i).getText());
Add parent node in locator to narrow down the search scope of encounter type and note type.
The reason why you get empty on SEEN BY, I guess it's not visible on page or there are more than one elements be found by your locator and the first element has no text content. If it's not visible, you can use getAttribute("innerText") to get text content of element.
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver
.findElement(By.cssSelector("div.col-encounter-type div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("NOTE TYPE: " + driver
.findElement(By.cssSelector("div.col-extra-padding div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("DATE: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-date']"))
.getText());
System.out.println("AGE OF ENCOUNTER: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-age']"))
.getText());
System.out.println("SEEN BY: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-seen-by']"))
.getAttribute("innerText"));
Related
Ho do I make unique xpath to let the robot locate into second field?
i used this but failed:
Click //div[contains(#class,'ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search') and contains(text(),'Select Source(s)')]
First Field:
<div class="ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search">
<div class="ant-select-selector">
<div class="ant-select-selection-overflow">
<div class="ant-select-selection-overflow-item ant-select-selection-overflow-item-suffix" style="opacity: 1;">
<div class="ant-select-selection-search" style="width: 3px;">
<input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_9_list" aria-autocomplete="list" aria-controls="rc_select_9_list" aria-activedescendant="rc_select_9_list_7" value="" id="rc_select_9" style="opacity: 0;" aria-expanded="false" readonly="" unselectable="on">
<span class="ant-select-selection-search-mirror" aria-hidden="true"> </span>
</div>
</div>
</div>
<span class="ant-select-selection-placeholder">attribute(s)</span>
</div>
</div>
second field:
<div class="ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search">
<div class="ant-select-selector">
<div class="ant-select-selection-overflow">
<div class="ant-select-selection-overflow-item ant-select-selection-overflow-item-suffix" style="opacity: 1;">
<div class="ant-select-selection-search" style="width: 3px;">
<input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_10_list" aria-autocomplete="list" aria-controls="rc_select_10_list" aria-activedescendant="rc_select_10_list_0" value="" id="rc_select_10" style="opacity: 0;" aria-expanded="false" readonly="" unselectable="on">
<span class="ant-select-selection-search-mirror" aria-hidden="true"> </span>
</div>
</div>
</div>
<span class="ant-select-selection-placeholder">Select Source(s)</span>
</div>
</div>
this xpath works (when I test at chrome developer console, it is detected) but when robot executed the script, it does not really click into the field. the drop-down list does not display.
Click //div[#class="ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search"]
error:
FAIL
Message: TimeoutError: locator.click: Timeout 10000ms exceeded.
=========================== logs ===========================
waiting for selector "//span[contains(#class,'ant-select-selection-placeholder') and contains(text(),'Select Source(s)')] >> nth=0"
selector resolved to hidden <span class="ant-select-selection-placeholder">Select Source(s)</span>
attempting click action
waiting for element to be visible, enabled and stable
element is not stable - waiting...
element is visible, enabled and stable
scrolling into view if needed
done scrolling
checking that element receives pointer events at (1080.4,304.7)
<div class="ant-select-selection-overflow">…</div> intercepts pointer events
retrying click action, attempt #1
waiting for element to be visible, enabled and stable
element is visible, enabled and stable
scrolling into view if needed
done scrolling
checking that element receives pointer events at (1080.4,304.7)
[ Message content over the limit has been removed. ]
element is visible, enabled and stable
scrolling into view if needed
done scrolling
checking that element receives pointer events at (1080.4,304.7)
<div class="ant-select-selection-overflow">…</div> intercepts pointer events
you are not so far from solution to select the uniq div
text equals:
//div[contains(#class,'ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search') and .//span[text()='Select Source(s)']]
if you want the expression contains:
//div[contains(#class,'ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search') and .//span[contains(.,'Select Source(s)')]]
You can give an index according to your best match like
(//*[#class='ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search'])[2]
Try to use the span tag and parent keyword in the xpath to find the required div tag element.
As per the HTML Code, Xpath would be something like this.
//span[contains(text(),'Select Source')]/parent::div/parent::div/parent::div
I have a page with list of products on it.
This is how HTML DOM looks like for one product item:
<div class="module card listing-search-card js-product-card " id="product-entry-123" data-product-id="123" data-toggle-status="open" data-out-of-stock="" data-toggle-isbundle="false" data-load-prices-async="false">
<div class="product-entry__wrapper">
<div class="card__header">
<div class="promotion">
<div class="product-entry__right promotion-card__body on-promotion--banner-offer">
</div>
<a href="/Products/p/123" tabindex="-1">
<picture>
<img class="card__image mobile-img lazyload" src="/medias/image-mobile">
<img class="card__image desktop-img lazyloaded" src="/medias/image-desktop">
</picture>
</a>
</div>
</div>
<div class="product-entry__body-actions-wrapper">
<div class="product-entry__body card__body">
<h3 class="card__title">
Schweppes
</h3>
<div class="product-entry__summary card__description-wrapper">
<div class="product-entry__summary__list">
<div class="card__detail-wrapper">
<div class="product-entry__summary__item card__description-product-detail">
33 x 24</div>
<div class="product-entry__summary__item card__description-product-code">
<span class="product-entry__code">
123</span>
</div>
</div>
<div class="container-type">
box</div>
</div>
</div>
</div>
<div class="cta-container">
<div class="card__amount-wrapper ">
<div class="card__amount">
61,83 € <span class="base-unit">HT/CHACUN</span>
<p class="sales-unit-price is-price">
<span>soit</span> 10,00 €
</span></span></p>
</div>
</div>
<div class="add-to-cart__footer add-to-cart__action">
<div class="success-overlay">Add to cart</div>
<div class="add-to-cart__action--active">
<div class="form-quantity__wrapper quantity-action quantity-action__wrapper"
data-form-quantity-id="123">
<div class="form-quantity ">
<button class="form-quantity__decrease quantity-action__decr icon-Minus disabled" type="button"
tabindex="-1" aria-label="decrement" data-form-quantity-decrement="">
</button>
<input id="product-123" class="form-quantity__input form-control quantity-action__value js-
quantity-input-typing" name="product-123" type="text" value="1" maxlength="4" data-price-
single="10.00" data-price-currency="€" data-parsley-range="[1,9999]" data-form-quantity-times="1"
data-parsley-multiplerange="1" data-parsley-type="integer" data-parsley-validation-threshold="1"
required="">
<button class="form-quantity__increase quantity-action__incr icon-Add-to-list" type="button"
tabindex="-1" aria-label="increment" data-form-quantity-increment="">
</button>
</div>
<span class="form-quantity__update" data-form-quantity-success=""></span>
</div>
<div class="add-to-cart__total">
<button class="button button--primary js-addToCart" role="button" title="Add
to cart" data-product-id-ref="123" data-modal-trigger="" data-modal-target="#add-to-cart-modal" data-
modal-before-trigger="addToCart" data-component-id="product list" tabindex="-1">
<div class="button__text">
<span class="button__text-add js-added-price">Add</span>
<span class="button__text-to-cart js-added-price">to cart</span>
</div>
<span class="button__text js-added-price mobile-only">Add</span>
</button>
</div>
</div>
</div>
<div class="add-to-template">
<button class="add-to-template--button button js-addToNewTemplate" type="button" data-modal-
trigger="" data-modal-target="#add-to-template-modal" data-modal-before-
trigger="openAddToTemplateModal" data-product-code="123">
<span>Add to list</span>
</button>
</div>
</div>
</div>
</div>
I am calling this function:
isSortedAlphabeticallyAscending($$('div.js-product-card'));
And the function implementation is:
isSortedAlphabeticallyAscending(list) {
for (let i = 0; i < (list.length - 1); i++) {
let outOfStockCurrent = list[i].getAttribute('data-out-of-stock');
let outOfStockNext = list[i + 1].getAttribute('data-out-of-stock');
let idCurrent = list[i].getAttribute('id');
let idNext = list[i + 1].getAttribute('id');
console.log("outOfStockCurrent " + outOfStockCurrent + " " + idCurrent);
console.log("outOfStockNext " + outOfStockNext + " " + idNext);
let productIdCurrent = idCurrent.split('-').pop();
let productIdNext = idNext.split('-').pop();
let currentText = list[i].$('a[href*="' + productIdCurrent + '"]').getText();
let nextText = list[i+1].$('a[href*="'+ productIdNext + '"]').getText();
console.log("currentText " + currentText);
console.log("nextText " + nextText);
if(outOfStockCurrent === "true" || outOfStockNext === "true") continue;
if (currentText > nextText) return false;
}
return true;
}
I ignore out of stock products since they are always at the bottom of the page.
But the list[i].$('a[href*="' + productIdCurrent + '"]').getText() is always returning empty text.
I would like it to get "Schweppes" text, i.e. product name.
Is there a way to chain somehow differently part with .$a[href ...] to get the text from the <a> tag inside the <div> element of the list of products using webdriverio 5?
Thanks!
The above selector list[i].$('a[href*="' + productIdCurrent + '"]').getText() targeted 2 elements.
What I needed to go one div further and find it there:
list[i].$('div.product-entry__body-actions-wrapper').$('a[href*="' + productIdCurrent + '"]').getText()
And voila, text appeared :)
Hope it will help someone with the similar issue :D
I am new to selenium,Guys please help me to click this element " Search Intimation View-Details".Cant able to use the ID has it is in number,& class name is not pointing exactly to that button.Guide me please,I'm strucked up.I tried
driver.findElementByXPath(" //div[span='Search Intimation View-Details'] ").click();
//
driver.findElementByClassName("v-tree-node v-tree-node-expanded v-tree-node-root v-tree-node-last ").click();
Below is the code
<div class="v-tree-node v-tree-node-expanded v-tree-node-last" id="gwt-uid-36" role="treeitem" aria-selected="false" aria-labelledby="gwt-uid-35" aria-level="2" aria-expanded="true">
<div class="v-tree-node-caption">
<div id="gwt-uid-35" for="gwt-uid-36">
<span>Intimations</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group">
<div class="v-tree-node v-tree-node-leaf v-tree-node-leaf-last" id="gwt-uid-38" role="treeitem" aria-selected="true" aria-labelledby="gwt-uid-37" aria-level="3">
<div class="v-tree-node-caption v-tree-node-selected">
<div id="gwt-uid-37" for="gwt-uid-38">
<span>Search Intimation View-Details</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group"></div>
</div>
</div>
</div>
I believe you are using Java selenium binding, so Use this code
driver.findElement(By.xpath("//span[normalize-space()='Search Intimation View-Details']")).click()
The below script uses the Java programming language and uses the CSS locators to find the required element if we are unable to find using the class, id, XPath, etc.
Use:
driver.findElement(By.cssSelector("div[role=treeitem][id^='gwt-uid-36']")).click();
I have one check box and two links with same classes & same Div.During the automation testing using protractor, i want to click on check box but it click on Links.
i am writing this code but its not working, please provide a solution.
Code:
element(by.id('Remember')).click();
[1
please find HTMl code:-
<div class="input-field">
<div class="pas_rembr">
<input name="remember" id="Remember" class="css-checkbox ng-dirty ng-valid-parse ng-touched ng-not-empty ng-valid ng-valid-required" ng-model="rememberMe" type="checkbox" ng-required="true" required="required">
<!-- <label for="Remember" class="css-label">I agree with the <a class="text_link" target="_blank" ng-href="{{baseUrl}}terms-conditions">Terms & Conditions</a>.</label> -->
<label for="Remember" class="css-label">I have read and I agree with <a class="text_link" target="_blank" ng-href="/lmd/terms-conditions" href="/lmd/terms-conditions">Terms and Conditions</a> and the <a class="text_link" target="_blank" ng-href="/lmd/privacy" href="/lmd/privacy">Privacy Policy</a> of this site.</label>
</div>
<span class="errorForm ng-hide" ng-show="(memberForm.remember.$dirty || submitted) && ((memberForm.remember.$error.required))">
<span class="errorForm ng-scope ng-hide" ng-show="memberForm.remember.$error.required" translate="TERAMS_CONDITION_IS_REQUIRED">Terms and condition is required</span>
</span>
</div>
Try this:-
var el = element(by.css('label[for="Remember"]'));
browser.actions().mouseMove(el, {x: 20, y: 3}).click().perform();
For your tests, the CSS selector would be
element = $$('label.css-label > a:nth-child(2)');
This should be able to click on the second a child of the label element.
Toggle button is present in an web application similar to
http://www.dhtmlgoodies.com/index.html?whichScript=on-off-switch
Search for "option two" in the above link.
Html code of the button is like this when turned Off
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable selected"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Html code of the button is like this when turned ON
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable selected"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation" checked="checked">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Used the following identifiers
//Case Association Turned On
#FindAll(#FindBy(how = How.CSS, using = "a.cb-enable.selected"))
public List<WebElement> caseAssociationIsON;
//Case Association Turned OFF
#FindAll(#FindBy(how = How.CSS, using = "a.cb-disable.selected"))
public List<WebElement> caseAssociationIsOFF;
While running using the selenium getting the error
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element
should have been "select" but was "span"
How do fix this. If more details are needed, please suggest.
I don't see any element that would match your selector, you should use label instead of a, like:
label.cb-enable.selected
and
label.cb-disable.selected
or use a selector based on input. a means link and you don't have any.
In case you still have the error what happens is that you must add an specific route:
In my case:
enter image description here
This is the resultant code:
enter image description here