selenium webdriver have to find element from dynamic dropdown - java

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;
}
}

Related

How to select an option from a dropdown using Selenium

I have this dropdown:
<select class="admin__control-select" data-bind="
attr: {
name: inputName,
id: uid,
disabled: disabled,
'aria-describedby': noticeId
},
hasFocus: focused,
optgroup: options,
value: value,
optionsCaption: caption,
optionsValue: 'value',
optionsText: 'label'" name="product[business_line]" id="N2JWY3F" aria-describedby="notice-N2JWY3F"><option value=""> </option><option data-title="PU - 21" value="425">PU - 21</option><option data-title="PU - 35" value="430">PU - 35</option></select>
The XPath is:
//*[#id="N2JWY3F"]
It has 2 options available: PU - 21 and PU - 35. I want to select the option: PU - 21.
I did this:
First, click on that dropdown:
driver.findElement(By.xpath("//*[#id=\"N2JWY3F\"]")).click();
How can I specify the option that I want? I tried different things and not one worked.
I'm guessing the drop-down isn't being clicked properly; can you check if "/*[#id="N2JWY3F"]" has only one match, and if so, maybe you can experiment with some other attributes:
driver.findElement(By.id("N2JWY3F"));
OR
driver.findElement(By.name("product[business_line]"));
OR
driver.findElement(By.className("admin__control-select"));
If updating the locator does not work, make sure the page is loaded and the drop-down menu is enabled before clicking. You can either wait or make sure the drop-down menu is visible on the page.
To select a value from the dropdown on the DOM you can utilize the Select class in Selenium.
Select drpDown = new Select(driver.findElement(By.id("N2JWY3F")));
drpDown.selectByVisibleText("PU - 21");
Basically first assign the dropdown to the Select class drpDown and then use one of the available methods to get the option you want.

Selenium NoSuchElementException Drop Down Menu

How can I get around the NoSuchElementException error message using Selenium in Python? I am trying to select a report type from a drop down menu and after running this code:
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.get("https://examplehtml.com/gims/app/reports")
##Report type
driver.find_element_by_xpath('//*[#id="reportType"]').send_keys("Power
Report")
This inserts the word "Power Report" but it does not select and move the page forward as it would if I manually selected the the report type and I think it's because of the NoSuchElementException error. Why is the element not being found and how can I get around this error. I'm fairly new to Selenium so any advice would help.
Thanks in advance!
Please, make sure, the element you trying to find is visible. Selenium cannot work on the invisible elements.
If the element you want is in a drop-down menu, first you need to drop the menu down. Then, try to find the element again.
I guess you need to use Select class that you import.
So if your HTML look like this:
<select>
<option value="1">Power Report</option>
<option value="2">Other Report</option>
</select>
.. then I would try this code:
e = driver.find_element_by_xpath('//*[#id="reportType"]')
Select(e).select_by_value('1')
.. in case of that the dropdown element is HTML select element.

How to loop through dropdown options, when each option opens new link

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

How to choose option from DropDownList when select not working

I have this simple DropDownList:
<select id="cmp_pp" name="cmp[val_id]" class="jcf-hidden"><option value="false" selected="selected">No</option>
<option value="true">Yes</option></select>
As you can see this DropDownList contains only 2 options: Yes or No.
And i try to select option this way:
val dropDownList =
new Select(
driver.findElement(By.cssselector("select[id=cmp_pp]")))
And i try all the following:
dropDownList.selectByVisibleText("Yes")
dropDownList.selectByIndex(1)
dropDownList..selectByValue("true")
And none of them works.
I found another way to change this DropDownList:
Open the DropDownList by click and then loop over all the options and click on the desire option that i want but my question is if there is another elegant way to do that ? (maybe java script ?)
Did you tried by using sendkeys? if select command does not works then good to use sendkeys.
driver.findElement(By.cssSelector("div.cmp_pp")).sendKeys("Yes");
I hope element is not in frame and tried with required waits, other locators as well instead of css.

Selenium Select Can't locate options

I am having an issue when I locate and assign a Select object grabbing its options, probably due to some of the wonky HTML on the page. Here is the HTML of the Select and its options:
<select>
<option selected="" val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
<option val="5">5</option>
...
</select>
The select is located using XPath as such: .//*[#id='employeeTable_paginate']/div/select
I am wondering if it isn't able to locate the options since the values are referred to as vals in the HTML? I tried the following code to see if it would get the Options:
for (WebElement option : select.getOptions()) System.out.println(option);
but it does not print anything. Additionally, if I try to select an option by index, it says it can not locate option with that index.
selectByValue would definitely not work in that case. If you go to the implementation of Select, you can see that selectByValue() uses xpath to find the value field. The good news is that it's an easy fix.
For your case, you'll want to find the single value (searching for val instead), and select it.
WebElement option = element.findElement(By.xpath(
".//option[#val = '" + value + "']"));
if (!option.isSelected())
{
option.click();
}
I'm not sure why selectByIndex doesn't work, can you check the count of select.getOptions()? It's always possible the select object is just incorrect.

Categories

Resources