How to get the attribute of Title in the input element
<input type="image" title="Previous Page">
<input type="image" title="First Page">
<input type="image" title="Next Page">
<input type="image" title="Last Page">
What have you tried? Typically something like the following should work:
WebElement element = driver.findElement(By.tagName("input"));
String title = element.getAttribute("title");
The answer provided by Jim Evans is the correct one imo, but for a more specific one i'd advise something like below. Remeber that copy-pasta might not work and you need to change something to be able to work on your full HTML.
List<WebElement> elements = driver.findElements(By.tagName("input"));
for (WebElement element : elements) {
if (element.getAttribute("type").equals("image")) {
System.out.println(element.getAttribute("title"));
}
}
The above code will loop for all the in your webpage that are from type="image" and print on the console the "title" attribute of each one of those.
Still thing you should vote Jim's answer as the correct one though.
First, you need to identify the input element from which you want to get the value of the attribute title .
Then something like the following must work.
element.getAttribute("title");
Its very simple and work for as well.
String title = driver.getTitle();
Related
Is it possible with thymeleaf to retrieve an input "generated" name attribute ?
I need to create a div with an id equals to an input name attribute.
Input names are generated via the th:field.
Markup:
<input type="radio" id="winterTiresInstalled" th:field="*{winterTiresInstalled.value}" />
Generated markup:
<input id="fld_winterTires" type="radio" name="vehicle.winterTiresInstalled.value" />
What I want :
<div th:id="???" />
to obtain :
<div id="vehicle.winterTiresInstalled.value" />
So, how can I retrieve the generated name attribute value "vehicle.winterTiresInstalled.value" with thymleaf ?
I could do it in js but I would prefer doing it in my template.
Thank you.
David
th-attr is best suited for this use case.
<a href="#" th:attr="data-id=${object.getId()}, data-name=${object.getName()}"/>
The above will result is something like this(haven't tested)
<a href="#" data-id="420" data-name="user-link"/>
Did the solution in javascript for now. I change the id of div after the page loads.
I would prefer do it directly in my template but can't figure out a way to do it with thymeleaf.
I have a submit button with text and I want to check for both.
i.e. the actual page has:
<input class="button" type="submit" value="Continue" tabindex="3"
name="nav[save]"></input>`
So I've tried
it "I can see the Continue button" do
expect(page.find('input[type="submit"]').find('input[value="Continue"]')).to be
end
but I get
Unable to find css "input[value=\"Continue\"]"
and
expect(page.find('input[type="submit"]')).
to have_selector('input[value="Continue"]')
but I get
expected to find css "input[value=\"Continue\"]" but there were no matches
If you want to check for both you can do it in single css selector like
input[type=\"submit\"][value=\"Continue\"]
Here's one solution that works with your view:
expect(find('.button').value).to eq 'Continue'
In the console:
[1] pry(#<Cucumber::Rails::World>)> expect(find('.button').value).to eq 'Continue'
=> true
I need to set custom attribute (data-validation-matches-message) value from messages resources.
<input data-validation-matches-message="Text from messages resources" />
I can receive and print messages resources value as:
<p th:text="#{user.notfound}"></p>
But how I can set this value for a custom attribute (data-validation-matches-message)?
UPD (I use this)
<input th:attr="data-validation-matches-message=#{user.notfound}"/>
Since Thymeleaf 2.1 you can do this:
<a data-th-attr="data-groupid=${somevalue}, data-groupname=${someothervalue}">...</a>
source
Try this:
<input th:attr="data-validation-matches-message='\'' + #{user.notfound}" + '\''"/>
Using 3.0.9.RELEASE:
<td th:text="${item.description}" th:attr="width=${isSplit} ? '44%' : '59%'" />
This will add width="44%" or width="59%according to a boolean set in the variables.
width might as well have been any other custom attribute.
I would put my JSP file in HTML5 format.
I have a problem with the tag struts radio button;
when I put cssclass"disabled" ( or other like cssClass"red" ) , when I look the source I get twice attribute class="disabled".
But It works nice for the other struts tag.
See example below:
JSP file:
<s:radio cssClass="disabled" name="mirror.swiBlo" list="Y"/>
source:
<input type="radio" name="mirror.swiBlo" id="consultation_mirror_swiBloY" value="Y" class="disabled" class="disabled"/>
If someone has any idea to solve that.
Thanks
Actually it's a bug in struts.
In simple/radiomap.ftl setting of class and style occurs twice.
source code:
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
</#if>
<#if parameters.cssStyle??>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
<#include "/${parameters.templateDir}/simple/css.ftl" />
And css.ftl handle class and style again.
So, here are two divs
<div class="th_pr"><input id="user_email" class="accounts_input" type="text" size="30" placeholder="Email" name="user[email]"></input><p class="accounts_error_text" style="display: block;">
email is invalid
</p></div>
<div class="th_pr"><input id="user_password" class="accounts_input" type="password" size="30" placeholder="Password" name="user[password]" autocomplete="off"></input><p class="accounts_error_text" style="display: block;">
password can't be blank
</p></div>
I need to get those elements with texts "email is invalid" and "password can't be blank" by text, cause it will differ depending on input.
I've been trying to complete this using xpath :
By.xpath("//p[contains(.,'email is invalid')]")
and
By.xpath("//p[contains(.,'password be blank')]")
but i get nothing.
resultEmail = ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[contains(.,'email is invalid')]")).apply(driver);
returns true, although the element is visible.
Please help.
Try xpath
//input[#id='user_email']/following-sibling::p
//input[#id='user_password']/following-sibling::p
Then you have
WebElement emailParagraph = driver.findElement(By.xpath("//input[#id='user_email']/following-sibling::p"));
System.out.println(emailParagraph.getText());
Did you try using the text() method within the xpath?
driver.findElement(By.xpath("//p[contains(text(), 'email is invalid')]"));
Rather than using the .?
Please try this:
WebElement userEmailErrorMessage = driver.findElement(By.cssSelector("div[class=\"th_pr\"]:nth-child(1) > p"));
WebElement userPasswordErrorMessage = driver.findElement(By.cssSelector("div[class=\"th_pr\"]:nth-child(2) > p"));
Using these elements you will be able to read the error messages for the respective input controls.