leniuNot able to enter value in text box of form in selenium webdriver - selenium

Hi I am using way2sms website and in send sms screen i am not able to enter the mobile number in mobile number field using selenium webdriver.
HTML Code: `
form id="smsFrm" name="smsFrm" method="post">
<input id="ssaction" type="hidden" name="ssaction" value="ss"/>
<input id="Token" type="hidden" name="Token" value="F448FDFD10E9288F9B4A204EF40EB29A.w803"/>
<div id="smilebox" style=" display:none;">
<div class="Sms fl">
<label>Mobile Number</label>
<div class="m91">
<span>+91</span>
<input id="mobile" type="text" onchange="javascript:dispLocMob(this);" onkeydown="javascript:dispLocMob(this);" onkeyup="javascript:dispLocMob(this);" value="" maxlength="10" placeholder="Mobile Number" name="mobile"/>
</div>`
Selenium Code :
`obj.findElement(By.xpath("//*[#id='sendSMS']/a")).click();
Thread.sleep(5000);
//obj.findElement(By.id("mobile")).sendKeys("8186867724");
obj.findElement(By.xpath("//*[#id='mobile']")).sendKeys("1234567890");`

Your element has an id, which is always the best way to address an HTML element using Selenium - very simple but still absolutely unique (bc otherwise it wouldn't be valid HTML). So try:
findElement(By.id("username"))
Full C# sample (should be similar in Java):
static void Main()
{
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
driver.Navigate().GoToUrl("http://site23.way2sms.com/content/index.html");
IWebElement element = driver.FindElement(By.Id("username"));
element.SendKeys("000");
}

I switched to the frame and got it detected.
driver.switchTo().frame(driver.findElement(By.id("frame")));
driver.findElement(By.id("mobile")).sendKeys("123456");

Related

In Selenium Webdriver using Java should I have to use element.click() before element.SendKeys for input text box?

driver.get("<url>")
WebElement name = driver.findElement(By.id(..));
name.click();
name.sendKeys("<input text>");
In the above code is it required to have name.click() first , or is it optional as the field is located and it should be able to send the input without having to click on it first
<input> tag
The <input> tag specifies an input field where the user can enter data. The <input> element can be displayed in different ways, depending on the type attribute. As an example:
<input type="text"> (default value)
<input type="button">
<input type="checkbox">
<input type="email">
<input type="file">
<input type="hidden">
<input type="number">
<input type="password">
<input type="radio">
<input type="submit">
<input type="url">
This usecase
As your usecase involves sending a character sequence presumably it should be of the following type:
<input type="text">
Generally you won't have to invoke click() before invoking sendKeys() but ideally to send a character sequence you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("birtviewer"))).sendKeys("Richard Bernard");
However, incase the element is a dynamic element having onfocus event attribute which fires the moment that the element gets focus as follows:
<input type="text" id="fname" onfocus="myFunction(this.id)">
you may require the additional step to click within the element.

Cannot click on the element ..not able to identify it

<div class="navpage-header-content">
<form action="textsearch.do" role="search" method="GET" class="form-inline navpage-global-search ng-non-bindable" aria-label="Global Search" target="gsft_main">
<input name="sysparm_ck" id="sysparm_ck" type="hidden" value="052ab09a0fa90700fa38563be1050e0fea31866160e7a5e6e0fc925775df282f070903d6"><div class="input-group-transparent">
<input name="sysparm_search" id="sysparm_search" placeholder="Search" type="search" class="form-control form-control-search">
<label for="sysparm_search" title="" data-original-title="Search">
<span class="input-group-addon-transparent icon-search sysparm-search-icon"></span>
</label></div></form></div>
This is the HTML tag for the element.
WebElement ele1 = driver.findElement(By.xpath("//span[#class='input-group-addon-transparent icon-search sysparm-search-icon']"));
ele1.click();
But my script cannot locate the element and click it.
I tried actions , Java script executor , but I just cannot Click on the element.
Actually you are trying to locate the span, You should locate input tag to click on search textbox, it should be like this:
WebElement SearchBox=driver.findElement(By.id("sysparm_search"));
SearchBox.click();
You can also use the xpaths to locate it:
//*[#id='sysparm_search']
//input[#id='sysparm_search']
//input[#name='sysparm_search']
Use this code for xpath:
WebElement SearchBox=driver.findElement(By.xpath("//*[#id='sysparm_search']"));
SearchBox.click();
You can also use name to locate it
Use this code for name:
WebElement SearchBox=driver.findElement(By.name("sysparm_search"));
SearchBox.click();

How to locate this input field in selenium?

This is the html code of the page. I need to input some text in username field.
How do i do it ?
<div class="login_cred">
<label>Username*</label>
<input id="username" name="userName" tabindex="12" size="30" maxlength="30"
onfocus="getFocus(this.id);" autocomplete="off" oncopy="return false"
onpaste="return false" onkeypress="return disableCtrlKeyCombination(event);"
onkeydown="return disableCtrlKeyCombination(event);" type="text">
<label>Password*</label>
<input id="label2" name="password" tabindex="13" title="password" size="30"
onfocus="getFocus(this.id);" autocomplete="off" oncopy="return false"
onpaste="return false" onkeypress="return disableCtrlKeyCombination(event);"
onkeydown="return disableCtrlKeyCombination(event);" type="password">
<br>
<br>
I tried following code and it is giving an error saying element not found
driver.findElement(By.xpath("//input[#id='username']")).sendKeys("qa");
Try below :-
driver.get("https://retail.onlinesbi.com/retail/login.htm");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("(//a[contains(.,'CONTINUE TO LOGIN')])[2]")).click();
driver.findElement(By.xpath("//input[#id='username']")).sendKeys("test");
Full code :-
System.setProperty("webdriver.gecko.driver", "D:\\Workspace\\FluentWaitTest\\src\\lib\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://retail.onlinesbi.com/retail/login.htm");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("(//a[contains(.,'CONTINUE TO LOGIN')])[2]")).click();
driver.findElement(By.xpath("//input[#id='username']")).sendKeys("test");
If still not work then their can be some frame or other tag present which same attributes
This may be due to the cause that the element are not loaded when you are trying to open the url http://retail.onlinesbi.com/retail/login.htm. You have to click CONTINUE TO LOGIN button first then you get your input boxes.

Getting value of the element

I need to get the value of data-id in the HTML code below.
My attempted code is:
//p[contains(.,'Smart card')]/following-sibling::button[#data-id='633597015500043521']"));
My HTML code shown below:
<form class="select-card-form" novalidate="" method="post">
<input type="hidden" value="SmartCardSelect_3ca61d51-e601-40de-80fd-308bc47b52c6" name="FormName">
<input type="hidden" value="d79cf158-93ad-4c77-b4bc-516ce8b28302" name="CardId">
<div class="select-item ">
<p>Smart card 1</p>
<button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500043521">
<span>Select</span>
</button>
</div>
</form>
Solution for this (JAVA):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select-item > button"))));
String value = element.getAttribute("data-id");
Java code snippet
WebElement element = driver.findElement(By.xpath("//form[#class='select-card-form']//div[#class='select-item ']//button"));
element.getAttribute("data-id");
Hope this helps.

How to automate the bootstrap fileupload upload control using webdriver

I want to automate the file uploading process which is using a file upload control built in bootstrap.
I am doing the same using webdriver.
Below is my code, but unfortunately it is not working:
element=driver.findElement(By.xpath("//[#id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);
It is giving an element not visible error.
Here is the example of the bootstrap fileupload control which I am trying to automate-
Via JavaScript:
on this URL http://markusslima.github.io/bootstrap-filestyle/
Please see below style-
$(":file").filestyle({icon: false});
Ok. i think i solved it.
WebElement fileInput = driver.findElement(By.id("document"));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("document"));
js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
element);
fileInput.sendKeys(fileName);
the bootstrap-filestyle.js hide a input element so you have to move it to the visible area and then set it in the standard way.
so much trouble for such a simple solution.
Here is my original html code:
<span id="documentUpload">
<input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
<div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
<input type="text" class="input-xlarge" disabled="" autocomplete="off">
<label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
</div>
</span>