I am using selenium with java and want to fetch the value "3". I guess I have to use xpath but I am not sure what would be the syntax for that? the html code is given below:
<div class="p-panel halign-center">
<div>
<span class="p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw" data-tag="Transcript-Summary-No-Due-Date-Count">3</span>
</div>
</div>
You can fetch value 3 by following code
String value = driver.findElement(by.xpath("//span[#data-tag='Transcript-Summary-No-Due-Date-Count']").getText();
OR
String value = driver.findElement(by.xpath("//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw']").getText();
Also we can improve the xpath if more HTML code is available.
Happy coding~
This is for getting by class:
.//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw']
This is for getting by text:
.//span[text()='3']
and this is both
.//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw' and text()='3']
Related
Could someone help, please.
I need to grasp a number that's generated after each run. As this number changes after each run, I need to grasp it and write it to excel sheet. I'm using Xpath for this field but not sure how to make this field more generic so that it only catches the number that is generated last.
The Xpath I tried is shown below and the syntax that is changing is div[2] to div[3] :-
driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div[3]/div/section/section/article/div[2]/div/div/div/div[2]/div[2]/div[1]/div/div[2]/p")).getText();
driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div[3]/div/section/section/article/div[2]/div/div/div/div[2]/div[3]/div[1]/div/div[2]/p")).getText();
My HTML SOURCE code is :
<div class="card-details row"> <div class="pane base4"> <div class="card"> <div class="card-name"> <div class="card-number"> <h4>Card number</h4> <p>633597015500042861</p> </div> <a class="submit-btn uniform-button button-smaller button-orange" href="/my-account/replacement-card?cardId=1ce25b86-27e6-4ce8-8ef3-6576f9a0ae84"> </div>
Thanks and Regards,
Az.
Please try to use below xpath to retrieve the card number.
String cardNum = driver.findElement(By.xpath(".//div[#class='card-number']/p")).getText();
You are accessing the randomly generated card number from the <div class="card-number"> tag.
Hope this helps.
//h4[contains(text(),'Card number')]/following-sibling::p
or
div[h4[contains(text(),'Card number')]]/p
if occurrence of same xpath more than once in your code and you want to select the last one following is the way:
xpath[count(sameXpath)]
this will give you the last occurence of the xpath
Please do it like below:
// as per your given xpath
String FirstXpath = "/html/body/div/div[3]/div/div/div[3]/div/section/section/article/div[2]/div/div/div/div[2]/div[";
String SecondXpath = "]/div[1]/div/div[2]/p";
// make sure here i value is as per your application
for(int i=2;i<4;i++){
driver.findElement(By.xpath(FirstXpath + i + SecondXpath)).getText();
}
Just make sure value of i as per your div value change.
Update
As per html provided driver.findElement(By.xpath("//*[#class='card-number']/h4/p")).getText();.
I am trying to get the text content of the items inside the following 2 div elements. It work using absolute path:
element = driver.findElement(By.xpath(".//*[#id='page-wrapper']/section/section/div/div[2]/section/div/div/div/div[45]"));
data = element.getText();
System.out.println(data);
The text that I am trying to get is:
45
Se
Selenium
I am trying to get text using relative XPath, but it always fails.
I am trying use this combination:
element = driver.findElement(By.xpath("//div[contains(#class,'elemBox noclaim noclaim-tt') and contains(text(),'45')]"));
or
element = driver.findElement(By.xpath("//div[contains(#class,'elemBox noclaim noclaim-tt')]/div[contains(text(),'45')]"));
but still failed.
I have HTML code like this:
<div class="elemBox noclaim noclaim-tt text-right m-t-lg animated-add pulse-add animated pulse animated-add-active pulse-add-active" >
<div class="" ng-show="(filterTool(elementName.name)) || (name === '')">
<div class="text-right ng-binding">45</div>
<div class="text-left ng-binding">
<span class="elemSym ng-binding">Se</span>
<br/>
Selenium
</div>
</div>
What should I do?
By using contains(text(),'45'), only the first direct-child text node will be evaluated. This is the reason you attempted XPath failed to find the div; because the text '45' is nested 2 levels from the outer div :
//div[contains(#class,'elemBox noclaim noclaim-tt') and contains(div/div/text(),'45')]
Or you might want to try using . instead of text to evaluate the entire texts within the outer div as opposed to evaluating only first direct-child text node :
//div[contains(#class,'elemBox noclaim noclaim-tt') and contains(.,'45')]
I think, you don't need to use contains word inside xpath.
textRightElement = driver.findElement(By.xpath("//div[#class='elemBox noclaim noclaim-tt']/div[#class='text-right']"));
or if class text-right is unique, just use
textRightElement = driver.findElement(By.xpath("//div[#class='text-right']"));
.... if you really need to find the element with it's value,
textRightElement = driver.findElement(By.xpath("//div[text()='45']"));
I have multiple HTML <input> elements like this:
<input type="text" class="information">
<input type="text" class="information">
<input type="text" class="information">
After entering different texts (e.g. “hello” "hi" "hey") in these input elements and save it I am able to print out their value using element.getAttribute("value"), which gives “hello” "hi" "hey".
However, when I try to grab this input element using XPath
//input[#class='information' and #value='hello']
//input[#class='information' and #value='hi']
//input[#class='information' and #value='hey']
it does not work (can not identify element with the expression). Any idea why this happens or how to get the input element using XPath in this case? Thanks in advance!
Maybe not the best way to do it but
try finding it by Xpath as follows:
xpath=(//input[#type='text'])[2]
the [2] being the boxes number (1/2/3). Once you've found the box you can access its value.
IWebElement box2 = FindElement(By.XPath('//input[#type='text'])[2]'));
box2.getAttribute();
A better way to do this might be trying to select the " :nth-child(n) " of the div. Perhaps google up on that
As nullpointer wrote, you should first get the list of elements using //input[#class='information'] and then have a closer look at each element using getAttribute("value").
You won't be able to find the values via XPath bc they have been entered after loading the page. In order to find the value attributes with XPath, they would have had to be loaded with the page, like in <input type="text" value="hello">, which it isn't in your case.
I am having a weird requirement. I am having a div class named "expandThisSection". And I have
<div id="requiredAccessoriesContentPlaceHolder" class="expandThisSection">No required accessories are available for this product.</div>
<div id="requiredAccessoriesContentPlaceHolderMeasurement" class="expandThisSection"> </div>
<div id="optionalAccessoriesContentPlaceHolder" class="expandThisSection">No optional accessories are available for this product.</div>
<div id="optionalAccessoriesContentPlaceHolderMeasurement" class="expandThisSection"> </div>
<div class="expandThisSection">
<div style="width:95%">mytext</div>
<ul class="movePos">…</ul>
<div><b>test</b></div>
<div><b>abc</b> Get this text</div>
<div id="vendLogo">…</div>
</div>
<div class="expandThisSection">
<table>...</table>
</div>
I want the content of the div that has style of 95% width.That is value I want is "mytext". But I am not able to find out xpath for the same.
Also I want xpath for finding the div content just above div with id="vendLogo". That is I want "Get this text".
NOTE: ITS ASSURED THAT THIS Bold tag WILL CONTAIN "abc"
How to do it? I am using selenium with Java
Got the first one. Not able to get second one.
Code:
List<WebElement> currentSkuDescription = driver.findElements(By.xpath("//div[#class='expandThisSection']/div"));
for(WebElement currentItem: currentSkuDescription) {
WebElement descriptionBlock = currentItem.findElement(By.xpath("//div[contains(#style, 'width:95%')]"));
description= descriptionBlock.getText();
}
Try dropping the #class='expandThisSection' because the div you want does not have that class attribute (it's parent does). Also, an exact match may be possible.
By.xpath("//div[#style='width:95%']
By.xpath("//div[contains(#style, 'width:95%')]
Either of those should work. If you want to make sure you are grabbing the right one, relative to the parent, use XPath axes...
//div[contains(#class, 'expandThisSection')]/child::div[contains(#style, 'width:95%')]
If you to search for the div by a specific text (in this case 'mytext') this you can use this xpath:
"//div[text()[contains(.,'mytext')]]/ancestor::div[#class='expandThisSection']"
Or if you want by the styling, the you can use this xpath:
"//div[#style='width:95%']/ancestor::div[#class='expandThisSection']"
Note that the second xpath will only work where the inline styling is done on the tag.
For the second query, try this xpath:
"//div[#id='vendLogo']/preceding-sibling::div[1]"
To get the specific text you require, you can do the following:
String text = driver.findElement(By.xpath("//div[#id='vendLogo']/preceding-sibling::div[1]")).getText();
text.replace(div.findElement(By.tagName("b")).getText(), "");
Here is the sample webpage code
<div class="size1of2 fllt">
<div id="iad-service" class="tmargin2 rite fllt service-check"></div>
<div class="fk-font-13 fk-font-regular">hi</div>
</div>
I want to find the "class" element using Selenium WebDriver.
Here is the code I tried.
String abc = driver.findElement(By.xpath("//div[contains(#id,'iad-service')]/#class")).getText();
System.out.println(abc);
When I tried this code(//div[contains(#id,'iad-service')]/#class) in the XPath Checker Addon, I am getting this output.
tmargin2 rite fllt service-check
But using WebDriver, I am getting an error. I want the output to be the content of the class which is.
tmargin2 rite fllt service-check
Where am I doing wrong?
You need to fetch the div element, then retrive the class attribute value:
String abc = driver.findElement
(By.xpath("//div[contains(#id,'iad-service')]")).getAttribute("class");
You can use this:
WebElement id=wd.findElement(By.id("iad-service"));
String className=id.getAttribute("class");