Selenium WebDriver - Not able to Select Drop down with onchange Script - selenium

How do i select a drop down has a attributes like below in Selenium WebDriver. Element are located with cssSelector.
Once any option in drop down is selected from the dropdown, the page refresh with new contents.
<select onchange="this.form.submit();" name="testproject" style="font-size:80%;position:relative; top:-1px;" >
<option title="A" value="1"> A</option>
<option title="B" value="2"> B</option>
<option title="C" value="3"> C</option>
<option title="D" value="4"> D</option>
I tried with below option but unable to locate/select any option.
Select select = new Select(driver.findElement(By.cssSelector("*[name='testproject']")));
select.selectByVisibleText("C");
How to identify/select a option from the drop down. Please help me with this.
Thanks in Advance!

Since this is a cssselector, use By.cssSelector instead of By.name
Select select = new Select(driver.findElement(By.cssSelector("[name='testproject']")));
If you want to use By.name, then use like this:
Select select = new Select(driver.findElement(By.name("testproject")));
NOTE:- In both the cases above, I am assuming there is one element "select" with the class "testproject". But, if that's not the case then you have to use the first code for cssselector and change the code to ;
Select select = new Select(driver.findElement(By.cssSelector("select[name='testproject']")));

Related

Trying to click labels inside input

html code:
<input id="19eb0353-6208-4baa-8f89-f7af0a6edb91" type="date" placeholder="Date of Birth" value="" name="dateOfBirth" data-componentname="dateOfBirth" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" data-ddlabel="dd" data-mmlabel="mm" data-yyyylabel="yyyy">
ID is dynamic
Trying to enter the day month and year but not sure how to write it
I know this is the data but cant find anything on selecting these specific labels inside the input
data-ddlabel="dd" data-mmlabel="mm" data-yyyylabel="yyyy"
Tried Xpath, css selector but the ID is dynamic
You can use the below css.
input[type='date']
or the below xpath
//input[#type='date']
Screenshot:
when registration page opens (after clicking join now), you can use xpath like "//select[#id='nike-unite-date-id-mm']" to select date, month and year separately (like dropdown).
OR
you can directly point to specific date using xpath
"//select[#id='nike-unite-date-id-mm']//option[contains(text(),'10')].click()"
this will directly select the date, similarly can be done for months and year by changing #id
Please use below code.
I have tested, it worked for me.
driver.findElement(By.xpath("//input[#type='date']")).sendKeys("04/04/1989");
Please let me know if you face any error.
You can try this (example using python):
from selenium.webdriver.support.ui import Select
month = Select(driver.find_element_by_id('nike-unite-date-id-mm'))
month.select_by_visible_text("08")
day = Select(driver.find_element_by_id('nike-unite-date-id-dd'))
month.select_by_visible_text("21")
year = Select(driver.find_element_by_id('nike-unite-date-id-yyyy'))
month.select_by_visible_text("2019")
And use Select Method like this:
<option value="01"> 01 </option>
^^
select_by_visible_text("01")
<option value="01"> 01 </option>
^^^^^^^^^^
select_by_value("01")

Dynamic XPath- Selenium

I have the following dynamic xpath:
/html/body[#class='modal-open']/div[#class='container']/form[#id='form_application']/div[#id='interviewContent']/div[#class='modal-dialog']/div[#class='modal-content']/div[#class='modal-body']/div[#id='form_application:interviewContent']/div[#id='form_application:questionDropdown']/select[#id='form_application:j_idt473']
How can I make this unique for Selenium Web Driver? I was going to do a Starts-with method but this is pertaining to a drop down box where the values selected changes with each log in.
Page Source:
<br /><select id="form_application:j_idt473" name="form_application:j_idt473" class="form-control" size="1" style="width:250px;display:inline" onchange="mojarra.ab(this,event,'change','#this','form_application:questionDropdown form_application:dropdown_interview')"> <option value=""></option>
<option value="02">Alaska</option>
<option value="01">Alabama</option>
<option value="05">Arkansas</option>
<option value="60">American Samoa</option>
<option value="04" selected="selected">Arizona</option>
<option value="06">California</option>
""""
You can use css attribute selectors to match partial strings. I would get rid of that super complex xpath and use a contains selector instead to match a substring. Something like this should do the trick...
select[onchange*="form_application:questionDropdown form_application:dropdown_interview"]
As you mentioned in your Question that the xpath is dynamic and the HTML clearly shows the element as a <select> tag so we have to construct our xpath accordingly. I think you were quite right when you spoke about using Starts-with method as looking at the id and name attributes it seems to me though the attributes are dynamic but the string form_application: will be a constant. So we can use the following xpath :
Java
Select city = new Select(driver.findElement(By.xpath("//select[#class='form-control' and starts-with(#id,'form_application:')]")));
city.selectByIndex(2);
Python
select = Select(driver.find_element_by_xpath("//select[#class='form-control' and starts-with(#id,'form_application:')]"))
select.select_by_visible_text("Alabama")

Selenium Element

Not able to select a dynamic drop down PFB the html tag of the element.
Drop down dynamic ids:
page:frm:pageB:repeatUpper:0:repeat:1:ServiceSubtype
page:frm:pageB:repeatUpper:0:repeat:2:ServiceSubtype
HTML Code :
<td class="dataCell" style="border-top:1px solid #838282;">
<span id="page:frm:pageB:repeatUpper:0:repeat:1:DescPopup" class="DescServicePopupClone" style="display:none;"></span>
<span id="page:frm:pageB:repeatUpper:0:repeat:1:DescPopupHasResource" class="DescServicePopupHasResource" style="display:none;"></span>
<span id="page:frm:pageB:repeatUpper:0:repeat:1:DescPopupLOE" class="DescServicePopupLOE" style="display:none;"></span>
My code:
new Select(driver.findElement(By.xpath("//td[contains(#id, 'page:frm:pageB:repeatUpper:0:repeat:')]"))).selectByVisibleText("CAT-New Implementer");
Thread.sleep(18000);
getting the below error:
Caused by: org.openqa.selenium.NoSuchElementException: Unable to
locate element: {"method":"xpath","selector":"//td[contains(#id,
'page:frm:pageB:repeatUpper:0:repeat:')]"} For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
UPDATE from OP - part of the "real" HTML:
<select id="page:frm:pageB:repeatUpper:0:repeat:2:ServiceSubtype" name="page:frm:pageB:repeatUpper:0:repeat:2:ServiceSubtype">
<option value="">--None--</option>
<option value="CAT-New Implementer">CAT-New Implementer</option>
<option value="CAT-Repeat Implementer">CAT-Repeat Implementer</option> </select> </span> </td>
Try this one:
new Select(driver.findElement(By.xpath("//select[starts-with(#id, 'page:frm:pageB:repeatUpper:0:repeat:')]"))).selectByVisibleText("CAT-New Implementer");
I also changed "contains" to "starts-with" because that better suits your example.
My suggestion to you is to not use XPath. Prefer ID, then maybe name, and then CSS Selectors. Those 3 locators should be 99% of your find methods. XPath is complex, fragile, and slow. Stick to the simple things until you get them mastered and then move on to XPath when you absolutely must.
This code should work
Select dropdown = new Select(driver.findElement(By.id("page:frm:pageB:repeatUpper:0:repeat:2:ServiceSubtype")));
dropdown.selectByVisibleText("CAT-New Implementer");
Since you have an ID on the SELECT, use that. Because it is a SELECT, casting the WebElement to a Select gets you extra options only available for SELECTs as far as choosing OPTIONs. For your example, you can use either .selectByVisibleText() or .selectByValue() since they are equivalent in the HTML you provided.
EDIT 1
Another option is to look for a partial ID, ID ends with ($) X, but I don't know if that is unique on your page.
Select dropdown = new Select(driver.findElement(By.cssSelector("span[id$=':ServiceSubtype']")));
Yet another option is to increment a counter and insert that into your ID string.
int i = 0;
Select dropdown2 = new Select(driver.findElement(By.id("page:frm:pageB:repeatUpper:0:repeat:" + i + ":ServiceSubtype")));
// i++; each time you add something

How to retrieve a selected value from a list box?

HTML Content:
<select id="modalNewUser-body-information-timeZone" class="span12 ng-valid ng-dirty" ng-options="timezone for timezone in timezoneList" ng-model="newUser().timeZone" uid="timeZone" name="timezone">
<option selected="" value="">-- Select One --</option>
<option value="0">AST</option>
<option value="1">EST</option>
<option value="2">CST</option>
<option value="3">MST</option>
<option value="4">PST</option>
<option value="5">YST</option>
<option value="6">HST</option>
</select>
suppose I have selected 'EST' from the list box How can I retrieve it?
I am not able to retrieve with getFirstSelectedOption().getText()
Select select = new Select(driver.findElement(By.id("modalNewUser-body-information-timeZone")));
select.selectByVisibleText("EST");
String timeZone = select.getFirstSelectedOption().getText();
It is my understanding that you can only use Select elements when there is actually a 'select' tag. Often times, things that look like drop down boxes don't truly behave like Select elements. What if you tried to get the text this way (I'm C# here, but Java just has different capital letters for this, I believe):
string timeZone = driver.FindElement(By.XPath("//option[#value = '1']")).Text;
expected return: "EST"
You can try this:
$("#modalNewUser-body-information-timeZone option:selected").text();
The code provide by you is working fine for me.
Could you please update your selenium version and try again?
Following is the tested code:
Select select = new Select(abhiDriver.findElement(By.id("modalNewUser-body-information-timeZone")));
select.selectByVisibleText("EST");
String timeZone = select.getFirstSelectedOption().getText();
System.out.println(timeZone);

Using Selenium for selecting an option on a select with optgroup

I'm trying to select a value in a select element. I'm using Selenium RC (Java) to run the test cases. I understand that the code for selecting a value is given by:
selenium.select("locator", "value=REQUIRED VALUE")
I'm unable to select the desired value with the above code. I think it might be something to do with optgroup in the select source code. I do not get any exceptions, the command executes fine but looking at the page the required value is not selected.
Also, I cant use ids (instead of value) because there arent any. Here is the source code of the selector:
<select>
<optgroup label="Group1">
<option value="13">some value1</option>
<option value="25">some value2</option>
</optgroup>
<optgroup label="Group2">
<option value="18">REQUIRED VALUE</option>
<option value="34">some value3</option>
...
...
</optgroup>
</select>
Is there any way to select the required value using Selenium?
It would be great if we could avoid the option values (such as "18", "34" etc) because these numbers change later as the values change. For example, "REQUIRED VALUE" has a value -18 but if I were to delete this item and add it again its value would be different. Basically this drop-down box is dynamic.
The value for the required option in your example is actually '18'. Try the following:
selenium.select("locator", "label=REQUIRED VALUE")