How to locate the First name and Last name text fields? - selenium

I want to inspect the textboxes as well as button,Please help me.I want to use sendkeys function for textbox.
HTML Code :
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
<form action="/action_page.php">
First name:
<br>
<input type="text" id="username">
<br> Last name:
<br>
<input type="text" id="username">
<br>
<br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>

To send character sequence to the First name and Last name field you can use the following solution:
First name:
CssSelector:
form[action='/action_page.php'] input:nth-of-type(1)
XPath:
//form[#action='/action_page.php']//following::input[1]
Last name:
CssSelector:
form[action='/action_page.php'] input:nth-of-type(2)
XPath:
//form[#action='/action_page.php']//following::input[2]

Related

Using both .isDisplayed and .isEnabled?

Intro: I'm making automated tests with appium and I'm fairly new to it.
Newbie question: Is there any point making double assertions for elements using is.Displayed and .isEnabled?
Assertion .isEnabled means that the element is not disabled, to clarify this here is sample code:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
Enabled: <input type="text" name="fname"><br>
Disabled: <input type="text" name="lname" disabled><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
First input is enabled and second is disabled, but both of them are displayed. But it could be also like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
Not displayed and enabled: <input type="text" name="fname" style = "display: none"><br>
Displayed and enabled: <input type="text" name="fname" style = "display: inline-block"><br>
Not displayed and disabled: <input type="text" name="lname" disabled style = "display: none"><br>
Displayed and disabled: <input type="text" name="lname" disabled style = "display: inline-block"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Therefore there are different assertions for Selenium for different test cases. If you want a combination of both .isDisplayed and .isEnabled, use .elementToBeClickable. More information in documentation(JAVA)

How do i identify the 1st, 2nd and 3rd textboxes without using indexes(ex [1]) - xpath, selenium

Below is the code. can someone pls help me to identify 3 text boxes individually, without using indexes.
<html>
<head>
<title>test</title>
<script></script>
</head>
<body>
name:
<input type="text"/>
<br/>
name:
<input type="text"/>
<br/>
name:
<input type="text"/>
<div>testdiv</div>
</body>
</html>
You can try below expressions to match each input:
For the first one:
//input[not(preceding-sibling::input)]
For second:
//input[preceding-sibling::input and following-sibling::input]
For third:
//input[not(following-sibling::input)]

Not able to get Xpath post applying starts with

My HTML Structure:
<form id="assignment-submission-540296" action="/assignments/submit-assignment" enctype="multipart/form-data" method="POST">
<input id="assignment_id" type="hidden" value="540296" name="assignment_id">
<input id="assignment_user_id" type="hidden" value="131639273" name="assignment_user_id">
<input type="hidden" value="/assignments/view/540296/131639273" name="returnUrl">
<div style="margin-right:230px;margin-top:-2px">
<input class="by-button fileuploadImage" type="button" value="Submit" data_id="" style="margin-left:155px">
My XPATH Generated with FIREPATH Is this:
.//*[#id='assignment-submission-540296']/input[4]
in the above x path 540296 is dynamic value hence I did something like this. Modified XPATH:
.//*[starts-with(#id,'assignment-submission']/input[4]
but after this not able to identify the element.
You are just missing a closing parenthesis:
.//*[starts-with(#id,'assignment-submission')]//input[4]
HERE ^
Also, you needed to use // after finding the form to search for desired input anywhere inside the form.
Besides, you can make the xpath a bit more specific by specifying a form tag and relying on input type:
.//form[starts-with(#id,'assignment-submission')]//input[#type="button" and #value="Submit"]

capybara /cucumber can't find radio

I'm having an issue finding a radio button. Here is a snippet of the html:
<form action="/" id="frm-info" method="post"><input id="ClickedButton" name="ClickedButton" type="hidden" value="" /><input name="__RequestVerificationToken" type="hidden" value="KTQF3bkKPP0OirvtL1EYsW-Q77zq-8H9YAPqeoBB9ewpNSYoc0dOEout26qrtMmX6xBx0_roxqWRwCXAlwZRTyW9ZBTBjwNgifWqws6hyOFIRmc6O-7P6jZXbZNYJ5Pazt9Hmg2" /> <div class="row borGreyPad mlmrcolor bb0">
<div class="col-sm-12 coverImage">
<div class="col-md-7 col-lg-6 fr xs-fl">
<div class="frm-content axaborderBlue mt10">
<div class="pl25 pt15 pr15 pb10">
<p class="large-heading">Enter some basic information to get started</p>
<div class="row ">
<div class="row pl15">
<div class="col-sm-4 r xs-l mb5 f14">Application Taken: *</div>
<div class="col-sm-8 mb5">
<div class="groupBox">
<span class="dib f14 ">
<input id="ApplicationTaken" name="ApplicationTaken" tabindex="1" type="radio" value="ApplicationInPerson" /><span class="dib mr10 ">In Person</span>
</span>
<span class="dib f14 ">
<input id="ApplicationTaken" name="ApplicationTaken" tabindex="2" type="radio" value="ApplicationByPhone" /><span class="dib mr10 ">By Phone</span>
</span>
</div>
I want to select the radio button with name "ApplicationTaken" and value "ApplicationInPerson"
I've tried several different ways including:
When I click on the radio with name
"([^"]*)" and value "([^"]*)"$/ do |myName, myValue|
choose("#{myName}", :option => "#{myValue}")
end
and
When I click on the radio with name
"([^"]*)" and value "([^"]*)"$/ do |myName, myValue|
find(:xpath, "//input[#value='#{ myValue }']", match: :first).set(true)
end
I keep seeing the following error:
"Unable to find radio button "ApplicationTaken" with value "ApplicationInPerson".
I've also tried by ID, no luck. I CAN select a button on this page and fill in text fields, I just can't select radio buttons or drop downs. Thanks
Since you're using capybara try:
choose('Visible Text')
See:
https://gist.github.com/zhengjia/428105
For starters, you have two radio buttons with the same id. This is bad - you should not have duplicate ids on a page. Attempting to find an element by ID when there are duplicates is very unpredictable.
What's most likely happening is that it's finding the first element with a matching ID, and then checking the value attribute. When that doesnt match, it says the element could not be found, because it does not continue onto the next matching ID (because of the way id selectors work internally)
I see you're also using xpath to find the element. You generally should be using CSS instead of xpath for finding your elements.
So leaving the ID out, and using CSS instead, find('input[name=ApplicationTaken][value= ApplicationInPerson]') should get you the element you're looking for.

Accessing a file dialog inside an iframe

I have an inline jsp page which has the code for the html type: file. The below displayed IFRAME tag is in the main jsp. I want to pass on the file name and submit this file dialog using the web driver. Basically want to do something like:
WebElement elem = driver.findElement(By.id("attachmentfile"));
elem.sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");
However, am not able to get a hold of the attachmentfile id. Any help would be appreciated. Thanks.
Main Jsp:
<IFRAME id=fileupload src="fileupload.jsp?type=uploadbutton"
frameBorder=0></IFRAME></TD></TR></FORM>
The fileupload.jsp is as below:
<html> <body>
<form name="frm_fileUpload" ENCTYPE="multipart/form-data"><%
<tr>
<td>
<input type="file" name="attachmentfile"
id="attachmentfile" onChange="uploadFile ();" />
<input type="button" name="uploadbutton" id="uploadbutton"
value="Upload" class="button" />
</td>
</tr>
</table>
</form>
</body>
</html>