Selenium Select Can't locate options - java

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.

Related

Java Selenium - Select a WebElement based on value attribute

I was wondering if there was a way to find an element on a webpage based on the value attribute.
<option value="abc">Some text here</option>
<option value="bcd">Some text here</option>
I figured I could just create a list of WebElements based on the tag name and traverse each one using .getAttribute("value"), but I was wondering if there was a more effective way of doing this similar to the way you can find an element based on its text using:
driver.findElement(By.xpath("//*[contains(text(), '" + term + "')]"))
You can do this:
driver.findElement(By.cssSelector("[value=\"abc\"]"))
and you would change the value of value depending on what you were trying to find.
To locate the element with value="abc" on a webpage `based on the value attribute of abc you can use either of the following Locator Strategies:
css-selectors:
driver.findElement(By.cssSelector("option[value='abc']"))
xpath:
driver.findElement(By.xpath("//option[#value='abc']"))

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.

selenium webdriver have to find element from dynamic dropdown

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

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 WebDriver - drop down list contains only the first element when using HTLMUnitDriver

I am trying to select an element in Selenium 2 from a drop-down list using this code:
WebElement przyczynaZakProcesuSelect = driver.findElement(
By.name("j_id_t:j_id_41:0:j_id_4h:0:j_id_51:0:qstloop:0:select1" ));
Select sel = new Select( przyczynaZakProcesuSelect );
List<WebElement> lista = sel.getOptions();
System.out.println( "List --> count of elements :" + lista.size());
for( WebElement e: lista){
System.out.println( e.getText() );
}
sel.selectByVisibleText("yyyyyy");
Everything works fine while using InternetExplorerDriver, this code prints all 15 options to the console, and I am able to select last yyyyyy option. This is an output printed to the console for
InternetExplorerDriver
List --> count of elements :15
aaaaaa
…….
……
yyyyyy
However, when using HTMLUnitDriver, Selenium retrieves only the first option in the list, all remainig options are lost, and I am able to select only the first option, this is what is printed to the console when using HTMLUnitDriver:
List --> count of elements :1
aaaaaa
This is a snippet of HTML code:
<select id="j_id_t:j_id_41:0:j_id_4h:0:j_id_51:0:qstloop:0:select1"
onchange="jsf.ajax.request('j_id_t:j_id_41:0:j_id_4h:0:j_id_51:0:qstloop:0:select1',event,
{execute:'#form',render:'j_id_t:processContent j_id_t:ccpmScripts ccpmMessages ',
'javax.faces.behavior.event':'change'})"
class="selectboxNew frmLOK" onfocus="a4j_focus(this)"
title="aaaaa"
size="1" name="j_id_t:j_id_41:0:j_id_4h:0:j_id_51:0:qstloop:0:select1">
<option title="aaaaa" selected="selected" value="4650">aaaaaaaaa</option>
<option title="bbbbb" value="4643">bbbbb</option>
<option title="ccccc" value="4651">ccccc</option>
....
....
<option title="xxxxx" value="4647">xxxxx</option>
<option title="yyyyy" value="4649">yyyyy</option>
</select>
What I am doing wrong ? Is there any workaround ?
I need to run this test using HTMLUnitDriver, please help.
Make sure that you did
driver.setJavascriptEnabled(true);
If you did, you can try emulating specific browsers like:
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
You can also try co click on that select prior selecting values.
Make sure you understand the driver's behavior well - take a look at the documentation
Unfortunately my experience is that the emulated driver won't work properly for bigger applications (I mean JavaScript). Especially when there are some browser-specific workarounds.

Categories

Resources