I'm using Java and Selenium. I know I could get visible text using the getText() method/ For example, if I supply the ID (via XPath or CSS) I could do getText() on this to get visible text. But what if I want to do the opposite. Let's say I know what the visible text is and I want to use the text to find another attribute? For example, let's say I have the following HTML markup:
<div class="" title="Card"/>
<div id="99999cardName" class="cardName editInline" title="Click to edit">ZZZ</div>
<div id="99999cardNumber" class="cardNumber">4590 6565 6565 6565</div>
In this example, suppose I knew the value 4590 6565 6565 6565 and I wanted to get the value 99999cardNumber - how would I do it?
Thanks
Use xpath in such a way that it contains text of that element. Here's how to do it -
String idVal = driver.findElement(By.xpath("//div[contains(text(),'4590 6565 6565 6565')]")).getAttribute("id");
System.out.println(idVal);
Hope this helps.
Related
I am having a problem in clicking the link text given inside a span tag.
html code :
<div id="menu" style="width: 1752px;">
<div class="dd_menu" dd_event_id="dd_event_2">
<a class="dd_menu_menu_entry dd_menu_entry_clickable" href="javascript:void(0);" style="left: 3px; width: 111px;" dd_menu_id="0">
<a class="dd_menu_entry dd_menu_entry_clickable" href="javascript:void(0);" style="left: 114px; width: 131px;" dd_menu_id="1">
<span class="text" style="background-color: rgba(0, 0, 0, 0);">FirstMenu</span>
I need to click on the text 'FirstMenu' .
I have used the xpath : .//*[#id='menu']/div/a[2]/span
It does not seem to work. How do I fix it?
If your requirement is to "click on the link FirstMenu", then you should use that as the locator. No need to mess around with XPath.
driver.findElement(By.partialLinkText("FirstMenu")).click();
The .partialLinkText() locator strategy should account for any extra whitespace padding due to the extra span element.
Your xPath returns span element so you're clicking that span. To make your xpath return a link ament your query to the following:
//*[#id='menu']/div/a[span]
This query returns a "link" that has span element as a child.
Try to use below xpath :-
//span[contains(.,'FirstMenu')]
If it doesn't work then there may be any frame present. You need to switch it on first.
Please let me know if there is more element with name FirstMenu on DOM
Hope it will help you :)
The problem got solved by using the same xpath i specified above with the usual syntax driver.findElement(By.xpath("//*[#id='menu']/div/a[2]/span")).click(); after i gave the order by which testcases have to be executed and using #Test(priority=something) and giving some implicit waits.
Thank you all for the suggestions.
regards,
roma
It is not good to use xpath. If the html of the page is changed your code would stop working. Try with css selector.
This is is simple code and you can modify it for you case:
var collection = driver.getelementsBy(By.cssSelector('div#menu div'))
It should return you collection with elements
And after that you can iterate through collection and find the element you want to click.
Hope the answer helps you.
I'm trying to verify the tool tip text using selenium for the following HTML code.But not sure how to proceed further.
HTML CODE:
<div id="divImgAnnualAllowanceType" class="imgHelp" _tooltip="If the client
<br>an arrangement
<br>a durable arrangement
<br>received a Lump Sum.">
</div>
Since the text is separated by <br> I don't know how to retrieve this text.
I tried using the following code but got null value.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("value");
Thanks in Advance :)
<div> tags doesn't (necessarily) has value attribute, use getText() instead
driver.findElement(By.id("divImgAnnualAllowanceType")).getText();
You should specify the attribute which value you need.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
You have to use proper attribute name with getAttribute() method and there is no attribute as value in your targeted <div> element, you can try below code:
String tooltip = driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
System.out.println(tooltip.replace("<br>", ""));
In above code, we are storing the tooltip value in a variable then printing the same after replacing all <br>.
This code is written in Java.
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 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]