Selenium VBA select option input element - vba

I'm using Excel VBA, Selenium and Chrome.
Threse is a dropdownbox box and I'm trying to select an option. Html code is:
<div class="x-form-field-wrap x-form-field-trigger-wrap x-abs-layout-item x-trigger-wrap-focus" id="ext-gen437" style="width: 100px; left: 330px; top: 70px;">
<input maxlength="1" spellcheck="false" id="ext-comp-1233" name="CITTADINANZA" class="x-form-text x-form-field x-field-uppercase x-trigger-noedit x-form-focus" readonly="" style="width: 75px;" />
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt="" class="x-form-trigger x-form-arrow-trigger" id="ext-gen438" />
</div>
The dispalyed text options are "Si" and "No" but in Html code there are no options, index, value...(it's just an input element; the img element is the arrow you have to click for a dropdown)
I tried to fill the input element using:
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByIndex
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByValue
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByText
driver.FindElementByCss("#ext-comp-1233").Sendkeys
but I recive the error:
"Unexpected TagName Error.
Expected=select
Got=input"
...while Sendkeys doesn't work.
Do you have any idea?
Thanks :)

Try this:
obj.FindElementByXPath("xpath of element").AsSelect.SelectByText ("option text")
Note:
driver - name of the object you've created for webdriver.

The solution is to click on the drop down image, and when the dynamically created divs are displayed, click on those divs.

Related

How to click on hidden button via VBA Selenium Chrome?

I am trying to click on a button on a website using VBA Selenium via Chrome.
When I use VBA code to click the button, ElementNotVisibleError pops up:
VBA code:
Set CD = New Selenium.ChromeDriver
CD.Start
CD.Get Url
Application.Wait (Now + TimeValue("0:00:05"))
CD.FindElementByCss(".inline_cancel").Click
When I move the mouse to the place where button is, the button appears and HTML code style display changes to block and before it was none.
When set to "none" VBA cannot find the element.
When set to "block" VBA can detect the element.
HTML code:
<span class="edit-hover" style="display: none;"><input type="hidden" name="field_root_name" id="order_header_change_attachments_attributes__field_name" value="order_header_change[attachments_attributes][]" class="field_root_name"><input type="hidden" value="365995938" name="order_header_change[attachments_attributes][365995938][id]" id="order_header_change_attachments_attributes_365995938_id"><input value="false" class="delete-field" type="hidden" name="order_header_change[attachments_attributes][365995938][_delete]" id="order_header_change_attachments_attributes_365995938__delete"><span class="intent-field"><span class="attachment-intent-checkbox"><input name="order_header_change[attachments_attributes][365995938][intent]" type="hidden" value="Internal"><input class="intent_checkbox_field" id="attachment_365995938_intent" type="checkbox" value="Supplier" name="order_header_change[attachments_attributes][365995938][intent]">→ <label for="attachment_365995938_intent">Supplier</label></span></span><a aria-label="Delete Attachment" class="inline_cancel" href="javascript:void(0);" role="button" tabindex="0"> </a></span>
You saw it right. Clearly the desired element:
<a aria-label="Delete Attachment" class="inline_cancel" href="javascript:void(0);" role="button" tabindex="0"> </a>
have the ancestor <span> with attribute style="display: none;"
<span class="edit-hover" style="display: none;">
which implies the element is hidden i.e. either not visible or not enabled.
To click on the element you have make the desired element clickable by taking some actions on some other element first.

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

Unable to identify objects

My webelement is inside a frame and i need to traverse through lot of div's inside the particular frame to reach to my required element.
Could you guys help me out in identifying it.
Tried with SwitchTo(), css selector() but not able to fix it.
Thanks in advance
<iframe id="ext-gen472" class=" ux-mif" frameborder="0" name="mif-comp-109379" style="overflow: auto; width: 1370px; height: 268px;" src="/SM9QA/cwc/nav.menu?name=navStart&id=ROOT%2FService%20Desk%2FRegister%20New%20Interaction"> `<div id="X49Edit" class="mandatoryFieldStyle">
<input id="X49" type="text" scripttype="text" value="" onblur=" applyToSameControl(this); " onclick="handleOnClick(this, event);" onchange="handleOnChange(this, event);" onfocus="handleOnFocus(this, event);" onkeyup="handleOnChange(this, event);" maxlength="100" style="width:100%; height:100%;" tabindex="" sctype="Text" datatype="string" buttonid="" dvdvar="" name="instance/title">
`
try this code ....
List<WebElement> frameList=driver.findElements(By.tagName("iframe"));
driver.switchTo().frame(1);
System.out.println(frameList.size());
Hi Bhumi did you tried switching of frames.
try using -
driver.switchTo().frame(driver.findElement(By.id("ext-gen472")));
driver.findElement(By.id("X49"));

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>

Selenium Webdriver - (Java) - Click a button with dynamic ID

1) I have a dialog on my web page having 2 buttons, Yes & No.
2) IDs of these buttons are dynamicaly changing every time.
3) How to handle this situation and click on Yes button?
4) Both buttons, Yes & No, have same classname 1.e. rwInnerSpan
5) Here is the Xpath for Yes button
(.//*[#id='confirm1381468352443_content']/div/div[2]/a[1]/span/span)
the part 1381468352443 in xpath is dynamically changing.
Below is the source code of page
`
<tr class="rwTitleRow">
<tr class="rwContentRow">
<td class="rwCorner rwBodyLeft"> </td>
<td class="rwWindowContent" valign="top">
<iframe frameborder="0" name="confirm1381468352443" src="javascript:'<html></html>';" style="width: 100%; height: 100%; border: 0px none; display: none;" tabindex="0">
<div id="confirm1381468352443_content">
<div class="rwDialogPopup">
<div class="rwDialogText">
<div>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(true);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">Yes</span>
</span>
</a>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(false);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">No</span>
</span>
</a>`
Thanks in Advance !!
You can directly check for the text in your Xpath:
driver.findElements(By.xpath("//a[#class='rwPopupButton']/span/span[contains(text(), 'Yes')]"))
There is a way to locate objects using partial link text, so you can try this:
driver.findElement(By.partialLinkText("Yes")).click();
Plain By.linkText may not work because of additional spaces or characters in the link.
You can click on the button based on the Text. Following method will give you a webelement based on the class locator and text.
WebElement getElementBasedOnClassAndText(String classLocator, String text){
List<WebElement> elements = driver.findElements(By.className(classLocator))
for(WebElement element : elements){
if(element.getText().contentEquals(text)){
return element
}
}
Assert.fail("Unable to find any element with the said Text")
}
Once you get the element you can take any action on it.
Since it is dynamic I would look for the changed name:
//Find the dynamicly created ID
String dynamicID = driver.findElement(By
.xpath("//iframe[contains(#name,'confirm')]")
.getAttribute("name");
//Use that ID to find the Yes option
driver.findElement(By
.xpath("//*[#id='"+dynamicID +"_content']/div/div[2]/a[1]/span/span")
.click();