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");
Related
I'm trying to select a value from drop down list. I have checked all posts related to it but not able to find solution.
Here is my HTML code for drop down list:
<select class="select2 visible" data-val="true" data-val-number="The field ClientId must be a number." id="ClientId" name="ClientId" tabindex="-1" title="" style="display: none;">
<option value="">Client</option>
<option value="22">ABC</option>
<option value="7">ABC1</option>
<option value="18">ABC2</option>
<option value="27">ABC3</option>
<option value="26">ABC4</option>
<option value="31">ABC5</option>
<option value="12">ABC6</option>
<option value="19">ABC7</option>
<option value="72">DGX Client</option>
<option value="57">DS Sampler</option>
<option value="25">Group123</option>
</select>
the code which I written in Selenium Webdriver to fetch value:
#FindBy(id="ClientId")
WebElement clientDropDown;
waitTime = new WebDriverWait(driver,20);
waitTime.until(ExpectedConditions.visibilityOf(clientDropDown));
Select client=new Select(clientDropDown);
client.selectByVisibleText("DGX Client");
Error: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of [[ChromeDriver: chrome on XP (6fa8cbb25476bea9b789aff19a6edf)] -> id: ClientId] (tried for 30 second(s) with 500 milliseconds interval)
Operating under an assumption here.
See how your select has is hidden by the display: none style:
<select ... style="display: none;">
^^^^^^^^^^^^^^^^^^^^^
I assume this is because it is actually represented on a UI differently and this select is under-the-hood manipulated by the javascript whenever the actual dropdown representation changes.
If this is the case, you can either inspect what the actual dropdown looks like and use a combination of click() commands to open the dropdown and select the desired option (note that you would not be able to use Select class in that case as it is designed to be used for select elements only).
Or, you can make the select element visible and continue:
String js = "arguments[0].style.display='block'";
((JavascriptExecutor) driver).executeScript(js, clientDropDown);
Select client = new Select(clientDropDown);
client.selectByVisibleText("DGX Client");
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");
<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.
Could anyone please explain me how to avoid the duplicate enters of records in to the select box options during editing the page.I am using java to develop web application I am very new to that,I need to remove the duplicates from the select box option while editing.Could anyone please help me .
Sample Code
<td><select property="employeestatus" value="" id="employeestatus">
<c:if test="${employee.employeestatus!=null}">
<option value="${employee.employeestatus}">${employee.employeestatus}</option>
</c:if>
<option value="">Select</option>
<option value="Active">Active</option>
<option value="Terminated">Terminated</option>
<option value="Deceased">Deceased</option>
<option value="Resigned">Resigned</option>
<option value="InActive">InActive</option>
</select>
<html:errors property="employeestatus"/>
<p id="employeestatus" style="color:red"/>
</td>
I would suggest to add the selected attribute on the option corresponding to your "employeestatus" variable instead of duplicating this option on the first line.
See HTML option selected Attribute
By the <html:errors ...>, I gess you use Struts 1. If it's true, I would use the
<html:select ...> tag and
<html:options or optionsCollection ...>
with java Map store in some context (servlet, session or request) to configure options. That struts tag will select the good option corresponding to your variable value.
See Struts <html:select ...> doc
How do i select the option Speedserver with HtmlUnit ?
It has no id or name so i dont know how to select it.
I read something about xPath but how do i use it ?
Here is the html code:
<select name="server" id="serverselector">
<option value="">Server</option>
<option value='s1.'>Server 1</option>
<option value='server2.'>Server 2</option>
<option value='speed.'>Speedserver</option>
</select>
OK, use getElementByID(#serverselector) to get HtmlElement by ID, then use:
getElementsByTagName("option") on it to get all the HtmlElements
with option tag and go through them with
getTextContent("Speedserver") and check the text content to be matched.
or, use getElementsByAttribute("option", "value", "speed") on it.