Populate the field in the HTML based on the text beside it using selenium - selenium

I am newbie to Selenium and trying to explore it. I am trying to populate the field in the HTML based on the text beside it.
For example in the below image, If I have to enter "Jack" as the First name in the text box, I must be able to search the HTML file for the text "First name:" and populate the text field beside it.
HTML Code:
<!DOCTYPE html>
<html>
<body>
<form>
First name:
<input type="text" name="firstname" >
<br>
<br>
Last name:
<input type="text" name="lastname" >
<br>
</form>
</body>
</html>
I understand it is possible if "First name:" is a label, we can use
selenium.type("css=label:contains(\"First name:\")+input", "Jack");
But the tricky part it is not necessary a label it can be any form such as
<h1>,<b>,<p> or even without any tags.
Any help is appreciated!
Thanks in advance!

Well, it sounds like you need to open up the html and take a look at what you're working with.
A good tool to use is Firebug plugin for Firefox browser, which allows you to easily select web elements and inspect them.
With Firebug, all you would need to do is right click the element you wish to inspect, and than click "Inspect with Firebug". Firebug will then highlight and point out the exact location of the web element within the HTML, allowing you to know what tag it may fall under.
Other than that, you could post the HTML of those fields and get some starting pointers on how you should write them into your tests.

Related

Unable to derive Xpath syntax for radio buttons without any distinguished value inside div tag

I want to click a radio button out of 10 radio buttons on a webpage but each radio button tag is exactly same inside Div/INPUT tag, i.e for each radio button values are exactly same and hence not able to derive a xpath to click on it. Can't use contains text to levarage radio button name as radio button name is in different SPAN tag so can't use that as a reference, Please help me: below is the code :
<div class="classname">
<input name="category.value" type="radio" class="classname">
</input>
<span class="classname">Radio button Name</span>
driver.find_element_by_xpath("//span[#class='classname']/input[#value=1]").click()
Your HTML must have some kind of return type, maybe value or name. I belive you can use that.
Mentioned in the comments can you please try:
//span[text()='Radio button Name']/preceding-sibling::input[1]
Not that this is case sensitive.
This works based on the html provided:
If it doesn't work for you can you please verify your HTML. There are lots of other ways of getting the element. If you need to modify the html just do it and let us know :-)

What could be a reason for not finding a web element by its id?

I have been using Robot Framework for a couple of days now to automate some basic flows tests in my working web app and found a problem:
Can't find some web elements using the id locator
I've been using the SeleniumLibrary for this. Tried with different locators such as xPath and CSS selector, setting an implicit wait before looking for the element and the keywords Wait Until Page Contains Element and Wait Until Element Is Visible, but still it gives the same result.
First of all I check if a certain element is present:
Page Should Contain Element id=some-button
then I try to send some keys to an input:
Input Text id=some-input Some characters
and then I get the error Element with locator 'id=some-button' not found
The page has the following structure:
...
<body>
<div>
...
<div>
<form>
<div>
<button id=some-button />
</div>
<div>
<!--- 8 opening div tags -->
<input id=some-input />
<!--- 8 closing div tags -->
</div>
</form>
</div>
</div>
</body>
...
Does any one have any idea why it happens?
UPDATE:
Tried
/html/body/div/div[1]/form/div[2]/div[1]/div[2]/div[2]/div[2]/div/div[1]/div[1]/div/input
instead of
//*[#id="some-button"]
and that worked perfectly.
What could be a reason for not finding a web element by its id?
These are the reasons I can think of off the top of my head:
the element truly isn't on the page at the time you're looking for it. This could be because you aren't waiting for the page to finish rendering, or it could be that it's genuinely not there.
you could have a typo in the id
the element is in a frame
the element was there, but then got deleted by some executing javascript
Whenever you're using any Xpath or a CSS selector, you should always check in it in Chrome Developer tools.
Steps:
1. Right click the page and click 'Inspect'
2. Go to Console tab
3. If it is a XPath evaluate using: $x and if it is a CSS selector, evaluate using $$
e.g.For
Xpath:
$x("//input[#id='Username']")
CSS:
$$("#Username")

Bootstrap Popover Link Inside a Label

On a form, I have a typical opt-in text checkbox but also have a link in that label. That link doesn't go anywhere but rather triggers a Bootstrap 3 Popover for additional detail.
It works, however clicking the popover link also unchecks the checkmark. Saw other questions on SO and solutions but they were for normal links and didn't apply to a javascript link link like this. Any thoughts?
The of course first thing one would think of is to have the link outside the label, but then the text won't wrap with the preceding text and will always have to appear beneath the disclaimer text which is not desired.
<div class="checkbox">
<label>
<input class="optin" type="checkbox" checked value="">My disclaimer text here. <a type="button" data-toggle="popover" title="Popover Title Here" data-content="Popover body text here">My Link</a>.
</label>
</div>
Assuming I need to integrate stop propagation somehow but can't seem to determine how to integrate in this scenario.

unable to identify tooltip message present for the textboxes in selenium

i am trying to automate the login functionality of a site.I want to verify whether the tooltip is present or not and to capture the tooltip text displayed for the textboxes.The tooltip is displayed when trying to click on the login btn without filling the textboxes.The tooltip text is attached to the input elements via bootstrap javascript.No tilte attribute is present for the textboxes
https://elasticbox.com/login/ is the site address.Any ideas on how to capture the tooltip text .Thanks in advance
This is not a bootstrap tool-tip as you commented for #Varun's reply.
This is just the HTML5 form validation which comes into action when you put "required" as the attribute of textfield.
Make an html file 'test.html' file using the below code:
<html>
<body>
<form name='form1' post="http://www.google.com">
<input type='email' required placeholder='email address please' />
<input type='password' required placeholder='password please' />
<input type='submit' value='button1' />
</form>
</body>
</html>
Herein, when you click on "button1" (after opening the file in browsers like: Chrome, Firefox, etc.), you will see the necessary validation under the textfield(s).
But, there is no possible way to inspect them.
You can, however, use Sikuli/Autoit to check the presence of that validation text, but that again will be a lost cause as the image of the validation messages/tooltip differs from one browser to another.
Looks like developer needs to be consulted for this.
In javascript it seems like the signin button will remain disabled until both values are filled i.e. username and password.
I am not much into javascript, may be you can consult the developer in order to understand this more.
You can refer image below:

Selenium: How to get plain text from html source?

i'm going to rewrite all my tests project, by replacing Selenium by HtmlUnit because i'm not able to get plain text in selenium as i can do with htmlunit using "HtmlPage:asText" method. Getting plain text can help me to verify easily the content of a page, without paying attention of the presence or not of the tags.
For example a plain text like this " One, two three" may correspond to many html source:
<p>One, two three</p>
or <table> <tr><td>One1</td><td>two</td><td>three</td> </tr></table>
or <div><span>One, </span> <span>two, </span> <span>three, </span> </div>
By using HtmlUnit i can write functional test without paying attention of how the actual content will be represented in the html format.
This will give you only plain text in page
String pageSource=driver.findElement(By.tagName("body")).getText();
Below logic will gives you entire page source.
driver.getPageSource();