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();.
Related
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']
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(), "");
I have the below code that I am attempting to extract a specific attribute from(data-id).
I am new to using selenium and have been pained by this for over a day now.
To add context to this I will give you a little background into what I am trying to achieve.
I have a webpage that has a auction, the auction has an ID, all items in the auction have unique ID's but all link to the original Auction ID.
I am attempting to extract the "data-id" attribute of an element however I have yet to find out how.
Here is a snippet of the code I am attempting to get the id from.
<div class="dropdown open">
<a class="dropdown-toggle form-control" href="#" data-toggle="dropdown">
<ul class="dropdown-menu dropdown-menu-form" role="menu">
<li id="liAuction4c42556772376a443736343d">
<label class="checkbox">
<input id="chkAuction4c42556772376a443736343d" class="auction" type="checkbox" data-caption="09-10-2015 10:30:00" data-id="4c42556772376a443736343d" checked="checked"/>
09-10-2015 10:30:00
</label>
</li>
</ul>
</div>
I have been on many forums and searched the whole of google and not found a solution that has worked for me yet otherwise I would not be posting the question and look like a complete Rookie.
I have attempted to use .getAttribute however I have had some issues with that and the code has never compiled, I guess I have not done something correctly.
String dataID = selenium.findElement(By.xpath("//*[starts-with(#id, 'liAuction')]")).getAttribute("data-id");
When I attempted the above the "findElement" part is underlined red and I have the following message,
"The method findElement(By) is undefined for the type Selenium".
If I change the parentisis around to look like this;
String dataID = selenium.findElement(By.xpath("//*[starts-with(#id, 'liAuction')]").getAttribute("data-id"));
"findElement" is no longer underlined, however now the ".getAttribute" part is underlined red and I have the following message, "The method getAttribute(String) is undefined for the type By"
I would really appreciate some assistance with this as I am about to throw my laptop out of the window, and I don't really want to lose all of my files.
Thanks in advance
Tony
You can use the getAttribute method.
First find the input element, then extract the data-id:
WebElement inputElement = selenium.findElement(By.id("chkAuction4c42556772376a443736343d"));
String data-id = inputElement.getAttribute("data-id");
Use below xpath:-
//input[#id='chkAuction4c42556772376a443736343d']/#data-id
Then use:-
String dataID = selenium.findElement(By.xpath("//input[#id='chkAuction4c42556772376a443736343d']/#data-id")).gettext();
Full Code:-
static WebDriver driver=null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.get("URL");
String dataID = driver.findElement(By.xpath("//input[#id='chkAuction4c42556772376a443736343d']/#data-id")).gettext();
System.out.println(dataID);
}
Hope it will help you :)
Thank you for your hekp with this guys, was not expecting to get the answer quite so quickly.
I needed to use the following line to retrieve the "data-id" attribute of an element.
String dataID = selenium.getValue("//input[#id='chkAuction4c42556772376a443736343d']/#data-id");
This ended up being alot easier that initially thought, I would like to thank #Shubham Jain for help with this his suggestion pointed me to where I needed to be.
I hope this helps others in the future
Hi I suggest you to use the following code. It will work absolutely fine.
WebElement element = selenium.findElement(By.xpath("//input[#class='auction']"));
String dataId= element..getAttribute("data-id");
Detail
I'm identifing the object using following xpath //input[#class='auction'] and save it to an WebElement variable.
then By using getAttribute() im getting the string from data-id
I have been working for quite a while on this and still haven't found an answer specific to my problem in stackoverflow or from experimenting with the Xpath myself. I am quite inexperienced so I alpogise if this is a simple problem but I would really appreciate any help.
I am working with Selenium to test a web app that uses Wicket. I need the Xpath to the checkbox that correlates to the respective label. This is because I need to be able to enter the value shown on the label and for it to find the relevant checkbox based on the label text such as "001", as the checkbox ids do not match the values.
Mockup below shows the checkboxes and their corresponding labels;
The corresponding HTML is show below;
<span wicket:id="excludeDepotCheckBox" id="excludeDepotCheckBox5">
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="0" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">001</label>
<br>
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="1" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">009</label>
<br>
</span>
Another problem I also face is that the Xpath must include the fact that it is inside the span shown in the html as there are 3 other groups of checkboxes on the page with the same values so it must be specific for each span for example:
id="excludeDepotCheckBox5"
I have tried the following Xpaths to no avail;
//span[#id='excludeDepotCheckBox5' and contains(., '009')]"
"//*[#id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::*[#name='adminPreferenceSection:excludeDepotCheckBox']
//*[#id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::input[1]"
Again I aplogise if it is a simple syntax/understanding problem but I would really appreciate any help.
Since "preceding-sibling" is so error-prone (it will break as soon as the HTML structure changes a little bit), here's a more stable variant (wrapped for legibility):
//span[#id = 'excludeDepotCheckBox5']//input[
#id = //span[#id = 'excludeDepotCheckBox5']//label[normalize-space() = '001']/#for
]
You can use the below xpaths:
1- For checking the checkbox related to label '001':
//span[#id='excludeDepotCheckBox5']/label[.='001']/preceding-sibling::input[1]
2- For checking the checkbox related to label '009':
//span[#id='excludeDepotCheckBox5']/label[.='009']/preceding-sibling::input[1]
NOTE: It will check for the 'input' element which is the first preceeding sibling of label element with exact innerHTML/text as '001' or '009' under a span element with id='excludeDepotCheckBox5'.
//*[#id='excludeDepotCheckBox5']/label[contains(text(),'001')]//preceding-sibling::input[1]