Im using selenium to test a web page which has a select option and one input type text.
Manually and using the selenium IDE, it works correctly, but when I export the test case to Java Junit, i can see the dropdown click, but selenium is not selecting the value, it is just expanding the dropdown.
What can i do?
Lets check my code:
Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");
Consider my form, like:
<form action="doit">
<select name="type" id = "type">
<option value = "fail"> Fail </option>
<option value = "celcius"> Celcius </option>
</select>
<input type="number" name="num" id="num">
</form>
Sometimes this method wont work. This may happen if the text between the option tags have spaces before and after the tags.
Eg.
1.<option> Fail </option>
2.<option>Fail</option>
In the above example 1 and 2 are different.
So you can use like,
driver.findElement(By.id("type")).click(); // this will expand the list
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
Also try to click directly. It work if the element is vivible,
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
driver.findElement(By.id("type")).sendkeys("Celcius");
Related
I am trying to build a custom build Jira Plugin, on the UI of plugin I am displaying select dropdown which consist of list of Issue Types.
I need to make option value "ALL" as default one for that select dropdown. I have written following code for that.
<label for="issue-types" >Issue Type(s):</label>
<select onchange="disableIfNoIssueTypeSelectedMandate()" class="multi-select" multiple="multiple" id="issue-types-mandate" name="issueTypes[]">
#foreach ($issueType in $allIssueTypesInProject)
<option value="$issueType.get("id")">$issueType.get("name")</option>
#end
</select>
</div>```
Can anyone please help me to know how to set default value in this select dropdown?
probably you should use <option selected="selected" value="$issueType.get("id")">$issueType.get("name") but with the default string values that you need
I have the following select.
I want to select specified elements with Java Selenium. I tried these but none of them working, I always got
In normal usage If I click on the element its
no such element: Unable to locate element:...
Heres the my code(s):
driver.findElement(By.xpath("//option[#value=\"2498847\"]")).click();
driver.findElement(By.xpath("/html/body/form/div/center/table/tbody/tr[2]/td[1]/font/select/option[2]")).click();
driver.findElement(By.xpath("//html/body/form/div/center/table/tbody/tr[2]/td[1]/font/select/option[2]")).click();
Tired with Select class:
Select selector = new Select(this.driver.findElement(By.xpath("//select")));
selector.selectByVisibleText("Harris_SF_1");
What am I doing wrong?
Or Is it possible to simulate a javascript click and call this script with Selenium?
Html:
<select size="10" name="DropDown" onchange="fSaveSearch(document.Form1.DropDown);">
<option value="1715424">1960 cypress creek N</option><!---this shows the saved searches list--->
<option value="2164877">Broker Closing Search</option>
<option value="2498847">Harris_SF_1</option>
<option value="1715426">Spring East</option>
<option value="1715428">Spring Klein</option>
<option value="1715429">Spring Klein Tomball</option>
<option value="1715427">Spring NE</option>
<option value="1715432">The Woodlands</option>
</select>
The final goal, to simulate a click on a option with specific visible text or value.
Edit: The site default iFrame is not that where these elements/js writter. If I change the iframe at the chrome console it works, but
driver.switchTo().frame("SearchFrame");
Doenst work am I change wrong?
Thanks Madhan your comment solved the my problem.
But it only worked after I swtich to Defaultcontent, then I switch to the frame I need!
driver.switchTo().defaultContent();
then switch to the frame I needed:
driver.switchTo.frame("Frame_ID");
I have a situation where I need to get the current selection from the drop down as a text or String and compare it for further assertion, but when I try the getText(), I get the complete list of items in the drop down, how can I get the currently selected item from drop down.
<form id="MODIFY_ATTRIBUTES" class="formList" method="post" action="jfn?ctxcontractId=L4%3A2274&mfunc=30&ctxnavCtx=C&cfunc=228&ctxaccountId=L4%3A1877&oid=L4%3A2042&ctx=L&octx=110&jfnRC=1">
<hr/>
<div class="formListTab">
<fieldset>
<div>
<label class="mandatory" for="p-param-L4:80000">Language</label>
<select id="p-param-L4:80000" name="p-param-L4:80000">
<option value="p-param-L4:80000-L4:12610">English</option>
<option selected="selected" value="p-param-L4:80000-L4:12600">French (Standard)</option>
<option value="p-param-L4:80000-L4:12830">Dutch</option>
</select>
</div>
I used the below XPATH, but it returns the complete list of items from the drop-down.
driver.findElement(By.xpath("//form[#id='MODIFY_ATTRIBUTES']/fieldset[1]/div/select")).getText();
You are fetching the text of whole Select element instead of just the selected option. You may use the inbuilt function to fetch selected option then fetch the text from this. Use Following:
String selectedOption = new Select(driver.findElement(By.id("p-param-L4:80000"))).getFirstSelectedOption().getText();
However if you want to use xpath. You may try following:
String selectedOption = driver.findElement(By.xpath("//select[#id='p-param-L4:80000']/option[#selected='selected']")).getText();
I would recommend Id to fetch your required text.
<td>Branch</td>
<td>
<select name="branch" type="text">
<option value="0">Select Branch</option>
<option value="CE">CE</option>
<option value="IT">IT</option>
</td>
<td>Batch</td>
i.e if i have branch field in my jsp form
so my branch firld contains 2 inputs as 1.CE & 2.IT.
So when i click on CE ,so it have to show me A1,A2,....A5 & when i click on IT, it will have to show B1...B5 from which we can select any one of the field.
So, pls help me for above program.
Here is my code.
Pls help me what code should i write in "Batch" field""
you have to register onChange() event of the branch and call an javascript function which will populate the batch field accordingly.
I am new to a Java environment and JSP. My question is, is is possible to detect the value of a select dropdown option and create a small if statement depending on this value?
For example:
<select id="my_select">
<option value="-1">Option 1</option>
<option value="0">Option 2</option>
</select>
<c:if test="${id eq -1}">
Show this
</c:if>
<c:if test="${id gt -1}>
Show this
</c:if>
Thanks guys!
No, you have to first realize the lifecycle of the JSP. The output is formed on the server and sent to the client. Then the client performs some actions and possibly submits a form back to the server.
In your case you have two options:
use javascript
submit the form holding the dropdown, and depending on ${param.id} show one or the other