How can I loop through each option in dropdown, while each option opens a new link on computer and after finishing working with the current option, I need to go to the next option of dropdown. Now, to get to the next option I am coming back to the window, where there is a dropdown and I am clicking next option from there. So, is it possible, to get to the next option right after I have finished working with the current one?
When option is selected, the linked document is opened in the parent frame, it means the link is opened in the same page. It has _parent tag, it acts the same like <a href="https://www.w3schools.com" target="_parent">
<select id="ab" name = "ab"
onchange = "javascript: var sel = getElementById('ab').selectedIndex;
var val = getElementById('ab').options[sel].value;
parent.location='http://ab/football/player_detail.php'+val;">
<option value='?rez=77777playerNid=1&controls=2:4:5'>Nike.com</option>
<option value='?rez=464677playerNid=4&controls=2:4:5'>diablo.com</option>
You can try following code:
UPDATED:
Select select=new Select(driver.findElement(By.id("ab")));
for(int i =0; i<select.getOptions().size();i++){
select = new Select(driver.findElement(By.id("ab")));
//selecting options
select.selectByIndex(i);
/*you can do nour work here depending upon option selected
* .
* .
* .
*/
driver.navigate().back();
}
Related
After selecting an option from drop-down. I am trying to get that option displayed in the console. below is my code. But I get
"//[[[[ChromeDriver: chrome on WINDOWS (d5a01776981da5dacfeb89dbbc2e6b52)] -> xpath: //*[#name='airline']]].// -> tag name: option]"
The tag name is option for dropdown options. I have tried all solutions of selectByXXXX. but nothing seems to work. what would be the right code ?
//airline preference
{
Select airline = new Select (driver.find Element(By.name("airline"))); //selecting tag
Thread.sleep(2000); //sleeptime`
airline.selectByVisibleText("Pangea Air"); //selecting option
Thread.sleep(2000); //sleep time
Select airlin = new Select (driver.findElement(By.xpath("//*[#name='airline']"))); //omg
WebElement s = airlin.getFirstSelectedOption();
Thread.sleep(2000);
System.out.println(s);
}
getFirstSelectedOption
getFirstSelectedOption() returns the first selected option in this select tag (or the currently selected option in a normal select). NoSuchElementException is thrown if no option is selected.
Seems you were pretty close. Once you select an option through selectByVisibleText() next you can invoke getFirstSelectedOption() to choose the option element selected and finally using getText() you can extract the option text as per the solution below:
Code Block:
Select airline = new Select (driver.find Element(By.name("airline"))); //selecting tag
airline.selectByVisibleText("Pangea Air"); //selecting option
WebElement s = airline.getFirstSelectedOption();
System.out.println(s.getText());
Console Output:
Pangea Air
I hope that someone could help me solving this.
Have to choose from dynamic dropdown only one which is active, disabled no. Following is part of dropdown.
<option value="1-2639425" disabled="disabled">21/10/2017 16:45 - Felipe Arantes - Josh Emmett (No odds available)</option>
<option value="1-2636744" disabled="disabled">21/10/2017 16:45 - Jim Wallhead - Warlley Alves (No odds available)</option>
<option value="1-2633126">21/10/2017 20:00 - Donald Cerrone - Darren Till</option>`
There are few things which confuses me:
I have to choose only the one(s) option which does not have 'No odds available' in it - they are clickable (those without 'No odds available')
It is not necessary that the one which can be chosen reside on 3rd position (as shown above) - can be first, second, 50th
Actually, I have to choose first clickable one (no matter where reside in option list)
Tried with this code but without any success:
Select dropdown=new Select(driver.findElement(By.xpath("xpath to 3rd option")));
dropdown.selectByIndex.selectByIndex(2);
Thread.sleep(5000);
Please assist.Thank you in advance.
You can get all the options using getOptions() and then get the first option without "No odds available" text or disabled attribute. Then you can use Select to select an option using the value attribute
Also, Select class doesn't get an option as parameter, it gets the parent <select> tag as parameter
Select dropdown = new Select(driver.findElement(By.xpath("xpath to the select tag")));
List<WebElement> options = dropdown.getOptions();
for (WebElement option : options) {
// if (option.getAttribute("disabled") != null)
if (!option.getText().contains("No odds available")) {
String value = option.getAttribute("value");
dropdown.selectByValue(value);
break;
}
}
I am trying to select a dropdown value which is inside a frame. This is the code
This is how we write for a link with in frame but i am unable to select a value from dropdown
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("right"));
WebElement el1 = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Text")));
el1.click();
First, wait for the correct frame (according to HTML code, the name of the frame is main_b)
Next, you don't have a link there (<a> tag), so By.partialLinkText cannot be used. Use By.name("field") instead
Finally, instead of clicking on it, get a Select object: Select mySelect = new Select(el1); and choose one of its options using selectByVisibleText, selectByValue or selectByIndex method
So all together looks like this:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("main_b"));
Select mySelect = new Select(
wait.until(
ExpectedConditions.elementToBeClickable(
By.name("field")
)));
// Select second option by visible text
mySelect.selectByVisibleText("Bacon Anna");
// Same option selected by value
mySelect.selectByValue("16344");
// Same option selected by index
new Select(el1).selectByIndex(1);
Try below code:
WebElement fr = driver.findElement(By.name("main_b"));
driver.switchTo().frame(fr);
WebElement dropdown = driver.findElement(By.name("field"));
Select sel = new Select(dropdown);
sel.selectByValue("<Value to be selected>");
You can also use wait commands if your page takes some time to load. Hope it helps.
Please look into the attached screenshot
I need to select all the data in 1st box at one time.Currently it allows me to select First name and then click on Arrow and then i need to click on Middle name and click on Arrow to move to box 2.I need to do individually for all the data in Box1 to move to Box2.Also i tried drag and drop Its not working Even manually also its not allowing to drag/drop from Box1 to Box2
Can we select all the elements in the box 1 at a time and then click on arrow to move to box2?Please help me out with this issue..
(Note : Below is the Html code for the Box 1)
`
As per provided comment xpath
/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option
will retrieve list of all options nothing but list of webelements. so use findElements here and collect that list
List<WebElement> elements = driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option"));
//use loop here
System.out.println(elements .size());
for(int i=0;i<=elements .size();i++) {
//use click on option
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option")).click(); //it will click first option by default
//write command to click on arrow
//so it will loop upto list size nothing number of options and always select first and clicks on >>
}
thank you,
Murali
You can achieve this solution very easy by Jquery.
the following code very useful to you.
$().ready(function()
{
$('#right_arrow_id').click(function()
{
return
!$('#firstboxid_here option:selected').clone(true).appendTo('#second_box_id_here');
});
for removing options from second box.
$('#left_arrow_id_here').click(function()
{
$('#second_box_id_here option:selected').remove();
});
});
By looking at the first xpath "/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option[i]", I understood that it is implemented like a dropdown. Hence, you can use "Select" class in selenium. Please find the below code for the same.
// Initializing the Select class
Select names = new Select(driver);
// Retrieving all the options in the dropdown
List<WebElement> allNamesList = names.getOptions();
// Looping through the list
for(WebElement eachName : allNamesList) {
// Clicking on each element in the first box
eachName.click();
// Your Code to click on Arrow button
}
Hope this helps.
My html page is divided in two and contains sidebar and content, in sidebar I have two dropdown boxes, one for school year and another for student name in respective school year selected in 1st dropdown and next is submit button. When I click on submit button it will redirect to next page and next page also looks same but in content page it shows selected student profile, in side bar the same two dropdowns will be there. The problem I am facing here is, first I will select school year and name. Its fine, when it goes to next page the dropdowns will be at initial stage but I want the selected year and name to be displayed on dropdown box, how can I do this?
Some one suggested me to keep the values selected in session after it goes to next page set the value of the dropdown.
Here is my code for dropdown,
<select style='width: 200px;'
id="combo_zone11" name="alfa1">
<c:forEach var="grade" items="${gradeInfo}">
<option id='syear' value="" selected="selected">
<option value=${grade.getDropDownId()}>${grade.getDropDownName()}</option>
</c:forEach>
</select>
How can I keep selected value in next page dropdown. please help me in this.
You could do this in many ways.
Since you're working in java, you can set the selected values to a hidden field, and while redirecting to next page you can set the values to request object.
Or if the target browsers support HTML5 localStorage, you can make use of it - on dropdown change, save the selected option to local storage using javascript. in the second pages load event, get the values from local storage and set the drop downs respectively.
Something like -
//Script in page1
window.onload = function(){
var dd = document.getElementById('combo_zone11');
dd.addEventListener('change',function(){
localStorage.setItem('ddValue', this.value)
});
}
//Script in page2
window.onload = function(){
var ddValue= localStorage.getItem('ddValue');
var dd= document.getElementById('combo_zone11');
for(var i = 0;i < dd.options.length;i++){
if(dd.options[i].value == ddValue ){
dd.options[i].selected = true;
break;
}
}
}
You can also make use of cookies, sessionStorage, queryStrings etc each method has it's own advantages and disadvantages...
Use select=selected by passing the id
like
<option value="${GroupLi.partyClassificationTypeId if_exists}" selected="selected"> ${GroupLi.description?if_exists}</option>
GroupLi is the list name used to iterate
as:
<#list Group as GroupLi>