I have a form where each text box has a title. I already have the WebElement of the text boxes, and I want to reference their title (the title that has "for=id" pointing to them).
I've tried their getText which only returns the text in the input box, tried getCssValue("label") which doesn't return with anything. I've tried finding all the labels but that doesn't help as I'd still have to sift through all of them, and find the 8 different labels that need to have the *.
<label for="customer_firstname">First name <sup>*</sup></label>
<input onkeyup="$('#firstname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_firstname" name="customer_firstname" value="">
I want to get back a string of the label text so I can check (with .contains())if it contains the "*"at the end. Preferably with as little Xpath as possible.
to indicate label tag using input tag, you can use this xpath :
//input[#id='customer_firstname']/preceding-sibling::label
if you just want input field then you should use id which I think is unique in your case, though you will have to verify this in DOM.
id = customer_firstname
For referencing with <label **for**>
you can use this code :
String custLabelAttribute = driver.findElement(By.xpath("//input[#id='customer_firstname']/preceding-sibling::label")).getAttribute("for")
this should print : customer_firstname
if both Input and label tags are belongs from same parent then you can take first unique xpath of parents and then adding // you can reach up to the label tag and get text. you should reach up to label tag by find unique xpath and use .gettext() then u will get innertext of label tag.
Related
How to handle this kind of pop-up. My goal is to get the message when Submit button clicked then validate it againts my own text (maybe using assert). I've tried to locate the element using firepath (xpath) but when i click locate Element button on firebug, the pop-up disappear.
Here is the screenshot of the pop-up.
popUp
Here is the code :
<p class="errors"></p>
<input id="email" class="form-control" type="email" value="" name="email" required="" oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Email Cannot Be Empty')" placeholder="Email *" data-placeholder="X" data-format="">
Thank you in advance.
From the scrrenshot it looks like a tooltip. Something like when we mouse over Google title in https://www.google.co.in/.
To verify tooltip we can get the attribute 'title' and verify.
Example : in https://www.google.co.in/. tooltip is placed in title attribute as below.
title="Google"
<div id="hplogo" style="background-size:272px 92px;height:92px;width:272px" title="Google" onload="window.lol&&lol()" align="left">
For your scenario, the displayed tip message is available in 'oninvalid' attribute as below. So get this attribute value and validate it.
oninvalid="this.setCustomValidity('Email Cannot Be Empty')"
If the element is not inside a iframe, then you can directly try as follows:
String emailId = driver.findElement(By.id("email")).getText()
// write string equals login here comparing emailId that is captured and the one you want to compare to.
if not, first find the iframeand switch to it and then use above code to find the element. More detailed answer related to switching b/w frame is here
Finding the elements in the Pop-up:
Instead of clicking on the Locate Element button (of Firebug) first, Right click on the element you want to find in the Pop-up, and select Inspect with Firebug, which gives the corresponding HTML code for the element.
This is a bit late in the game but the way you get the custom validity message and not the generic one you have to call the reportValidity() event within JavaScript in Selenium. You'll see a driver.executeScript() method and this is where you must call the reportValidity event on the element being validated. This is how I did it:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions
.ElementToBeClickable(By.CssSelector("input#NewPassword.form-control")));
IWebElement input = driver.FindElement(By.CssSelector("input#NewPassword.form-control"));
input.SendKeys(string.Empty);
IWebElement form = driver.FindElement(By.TagName("form"));
form.Submit();
driver.ExecuteScript("document.getElementById('NewPassword').reportValidity();");
Assert.AreEqual("New password required", input.GetAttribute("validationMessage"));
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'm using Selenium to test a webpage, and I can't seem to figure out how to write the XPath for the Next button.
There are multiple Next buttons in the code, however they're in different sections. The one I'm trying to test has the following code:
<section class="col-md-12">
<input type = "submit" value ="Next">
If I use the following command it gives me an unidentified element error.
//input[#type = 'submit']
OR
//input[#value = 'Next']
This is probably because it's viewing the other next buttons in the page. I'm not sure how to proceed.
This will select the input elements (with #value attribute value equal to Next) anywhere beneath section elements (with #class attribute value equal to col-md-12):
//section[#class='col-md-12']//input[#value='Next']
I need to read the text displayed on the popup window in Web driver using Java. I am able to handle the popup window for closing. I don't know how to read the text displayed on Popup window and print it in Console.
HTML :
<ul class="traceInner"><li class="traceHead"><span><input type="checkbox" class="chkall hand">奖期</span><em>追号倍数</em><em>追号状态</em><em>注单详情</em></li><li><span><input type="checkbox" rel="20151119031YCL02WY0001" class="hand disabled" disabled="disabled" name="2015316">2015316</span><em>1</em><em><label>已完成</label></em><em><a class="hand traceDetails" data-id="20151119031YCL02WY0001" rel="434">详情</a></em></li><li><span><input type="checkbox" rel="" class="hand" name="2015317" 追号中="">2015317</span><em>1</em><em><label>进行中</label></em><em> </em></li></ul>
I need to get the lable text of 已完成 in label.
MY CODE :
ConfirmationPlaceBet_Page.verification_ZhuiHao2(driver).getText();
1.You can use this xpath for general :
xpath = //label[text()= '已完成']
2.If you have too many ul element then use this xpath :
xpath = //ul[#class='traceInner']//label[text()= '已完成']
3.If you want to get first label element then use this xpath :
xpath = (//ul[#class='traceInner']//label)[1]
Note : Pass 2 instead of 1 if you want to get second label
4.If you want to get list of labels then use this xpath :
xpath = //ul[#class='traceInner']//label
Here is your answer of "How to get text" :
String text = driver.findElement(By.xpath("your Xpath")).getText();
Warning
For List of web elements you have to use like this :
String text_1 = driver.findElement(By.xpath("your Xpath of List")).get(0).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]