How to automate the bootstrap fileupload upload control using webdriver - file-upload

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>

Related

Selenium webdriver can't find xpath

element code:
<div class="col col-2"><div class="v-input v-input--is-focused theme--light v-text-field v-text-field--is-booted v-select primary--text"><div class="v-input__control"><div role="button" aria-haspopup="listbox" aria-expanded="false" aria-owns="list-65" class="v-input__slot"><div class="v-select__slot"><label for="input-65" class="v-label v-label--active theme--light primary--text" style="left: 0px; right: auto; position: absolute;">Narystė</label><div class="v-select__selections"><input id="input-65" readonly="readonly" type="text" aria-readonly="false" autocomplete="off"></div><div class="v-input__append-inner"><div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon notranslate material-icons theme--light primary--text">arrow_drop_down</i></div></div><input type="hidden" value="[object Object]"></div><div class="v-menu"><!----></div></div><div class="v-text-field__details"><div class="v-messages theme--light primary--text"><div class="v-messages__wrapper"></div></div></div></div></div></div>
in screen you can see, that entered xpath is only one.
Here is code, where i have entered the same xpath, but getting error like this:
private static final By naryste = By.xpath("//*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]");
#Step("Pasirenkame juridinio asmens organizaciją iš reikšmių sąrašo")
public createOrganization selectMembership() {
button.click(naryste);
return this;
}
Selenium webdriver can't find this xpath:
Find element :By.xpath: //*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]
P.S. the same problem if i choose other elements
AND this (selenium can't find)
To locate the element you can use the following Locator Strategy:
Using xpath:
By.xpath("//label[starts-with(#for,'input') and contains(.,'Narystė')]");
PS: Do add some waits before invoking the click()

Unable to find element in a form class or div class

I am unable to find an element in a form class or div class:
//Tried this clicking on Source name.
driver.findElement(By.id("__BVID__62")).click();
//Typing into Source name.
driver.findElement(By.id("__BVID__63")).sendKeys(new String[]{"CNN"});
I tried this:
driver.findElement(By.xpath("//input[contains(#class='form-control') and type='text']")).sendKeys("CNN");
HTML code:
<div class="card-body">
<fieldset id="_BVID__62" role="group" aria-labelledby="__BVID__62__BV_label" class="b-form-group form-group">
<legend id="_BVID__62__BV_label" class="col-form-label pt-0">Source Name</legend>
<div role="group" aria-labelledby="_BVID__62__BV_label" class="">
<input id="__BVID__63" type="text" class="form-control">
use this code :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement input = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='card-body']/descendant::div/input[contains(#id,'BVID')]")));
input.sendKeys("CNN");
As per the HTML you have shared the desired element is having a dynamic ID. So to send text to the <input> node you have to induce WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and starts-with(#id,'__BVID__') and #type='text']"))).sendKeys("CNN");

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();

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

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");

NoSuchElementException with unknown cause using Selenium

While writing selenium testcases for a webapplication I'm having trouble with the xpath selector. The element of the HTML-code which should be clicked on by Selenium is the following:
<a title="Voeg een vak toe" href="#" onclick="javascript:$.colorbox({width:818,href:'/olo-cleanjump/profiel/addVakForm'}); return false;">
<p class="add">
<img class="add-icon" src="/olo-cleanjump/static/images/icon_add.png"/>
Voeg vak toe
</p>
</a>
The Selenium IDE plugin for firefox gives me the following selenium code for this:
driver.findElement(By.cssSelector("p.add")).click();
The addVakForm javascript function that is called by this link opens a colorbox with the following HTML (I shortened it, there are around 30 similar div's with class "lesboek_popup") inside:
<div id="cboxLoadedContent" style="display: block; width: 776px; overflow: auto; height: 653px;">
<div id="profielpagina_add">
<h2>Voeg een vak toe aan je profiel</h2>
<div class="lesboek_popup">
<a class="content" href="/olo-cleanjump/profiel/addvak/120776">
<img src="" alt="">
</a>
<p class="caption">
Engels
</p>
</div>
<div class="lesboek_popup">
<a class="content" href="/olo-cleanjump/profiel/addvak/120786">
<img src="" alt="">
</a>
<p class="caption">
Biologie
</p>
</div>
</div>
For the test I want to open the 'Biologie' link. Selenium IDE got me the following selenium code to do this
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
to select this biology link element.
Based on this I wrote the following testcase:
Test
public void testAddRemoveVak() throws Exception {
this.get("");
// vak 1 toevoegen
driver.findElement(By.cssSelector("p.add")).click();
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
// vak 2 toevoegen
driver.findElement(By.cssSelector("p.add")).click();
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
assertEquals(driver.findElements(By.xpath("//li[#class='vak']")).size(), 2);
// vak 2 verwijderen
this.get("profiel/removevak/120791");
assertEquals(driver.findElements(By.xpath("//li[#class='vak']")).size(), 1);
}
The part
driver.findElement(By.cssSelector("p.add")).click();
actually was successful, so after this call the colorbox should be loaded. However the
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
causes an NoSuchElementException, while this element definitely is present in the colorbox when I check for myself (the call/xpath was even autogenerated by Selenium IDE). Does anyone have a clue what may cause the NoSuchElementException?
Probably you should wait when your popup appears. Try to use Implicit waits
WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Also instead of xpath you can use driver.FindElement(By.LinkText("Biologie")).click() if it the only link with text Biologie on your page