Selenium: how to remove selected attribute - java

I have this WebElement:
<select id="month">
<option value="empty">Select</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option selected="" value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
And I want to remove the selected attribute from Jul and put this attribute under different Month.
This is what I have tried to remove the Attribute:
(JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('selected', '')", myElement)

In general if you want to remove an attribute use the JavaScript removeAttribute method rather than trying to use setAttribute. In this case, though, you are working with a select and want to select a different element. Just use the built in Select class in selenium like this:
Select select = new Select(myElement);
select.selectByVisibleText("Aug");
I created an HTML file with the following contents and then tested it on that.
<html>
<body>
<select id="month">
<option value="empty">Select</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option selected="" value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
</body>
</html>
Then ran the following code:
#Test
public void testFoo() throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage()
.window()
.maximize();
driver.get("file:///blah/blah/blah/index.html");
Select select = new Select(driver.findElement(By.id("month")));
select.selectByVisibleText("Dec");
Thread.sleep(10_000);
}
The page ended successfully with "Dec" selected.

To deselect an option you can use deselectBy methods
WebElement dropdown = driver.findElement(By.id("month"));
Select select = new Select(dropdown);
select.deselectByVisibleText("Aug");
// or
select.deselectByValue("7");
// or
select.deselectByIndex(8);
// or
select.deselectAll();
And to select another month
select.selectByVisibleText("Feb")
// or
select.selectByValue("2");
See Select class

Related

Problem with dropdown list working with Selenium

I'm trying to select an element from a dropdown list, but I got this error in the execution:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element:
{"method":"xpath","selector":"//select[#id='id12']"}
The HTML code of the dropdown list is:
<select style="width:163px" name="department:department" id="id12">
<option selected="selected" value="">Escoge</option>
<option value="100">100</option>
<option value="999">999</option>
<option value="800">800</option>
<option value="700">700</option>
<option value="600">600</option>
<option value="540">540</option>
<option value="500">500</option>
<option value="400">400</option>
<option value="345">345</option>
<option value="280">280</option>
<option value="270">270</option>
<option value="264">264</option>
<option value="262">262</option>
<option value="251">251</option>
<option value="201">201</option>
<option value="82">82</option>
<option value="81">81</option>
<option value="50">50</option>
<option value="21">21</option>
<option value="19">19</option>
<option value="001">001</option>
</select>
My java code is:
WebElement dropdownlist = driver.findElement(By.xpath("//select[#id='id12']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
I've tried with this too:
WebElement dropdownlist = driver.findElement(By.xpath("//select[#name='department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
WebElement dropdownlist = driver.findElement(By.cssSelector("select[id='id12']"));
Possibly id is dynamic here and name attribute value you have provided is wrong.
Try the below code.
WebElement dropdownlist = driver.findElement(By.xpath("//select[#name='department:department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
To deal with dropdown you have to use org.openqa.selenium.support.ui.Selectimport and it will nstantiate the drop-down box as a "Select" object in WebDriver. Also it could be possible that element on which you are trying to perform action taking time and hence the exception occurred. Add Explicit wait and check from your end ::
WebElement element=driver.findElement(By.id("id12"));
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
Select dropdown = new Select(element);
dropdown.selectByVisibleText("100");

How to set the selected attribute of Select tab based on the servlet passed fields in JSP?

In my web application, I am working on a screen, where I want to display retrieved data onto the screen. In my screen I have one drop down for "direction". I am setting value of this field from servlet. But I am not getting a way to display the selected option by default in the page.
<select name="indv_adr_mail_st_dir" value="${indv_adr_mail_st_dir}">
<option value="EA">East</option>
<option value="NE">North East</option>
<option value="NO">North</option>
<option value="NW">North West</option>
<option value="SE">South East</option>
<option value="SO">South</option>
<option value="SW">South West</option>
<option value="WE">West</option>
</select>
What is the way to achieve this?
The reason this doesnt work is the <select> tags selected option is designated by placing the selected attribute on a specific option, not by setting the value attribute. For instance
<select name="test">
<option value="yes">Yes</option>
<option selected value="no">No</option> <!-- this is the selected option -->
</select>
For your code you could make this happen by using a ternary operator to optionally place the selected attribute on an option
<select name="indv_adr_mail_st_dir">
<option ${indv_adr_mail_st_dir=="EA"?"selected":""} value="EA">East</option>
<option ${indv_adr_mail_st_dir=="NE"?"selected":""} value="NE">North East</option>
<option ${indv_adr_mail_st_dir=="NO"?"selected":""} value="NO">North</option>
<option ${indv_adr_mail_st_dir=="NW"?"selected":""} value="NW">North West</option>
<option ${indv_adr_mail_st_dir=="SE"?"selected":""} value="SE">South East</option>
<option ${indv_adr_mail_st_dir=="SO"?"selected":""} value="SO">South</option>
<option ${indv_adr_mail_st_dir=="SW"?"selected":""} value="SW">South West</option>
<option ${indv_adr_mail_st_dir=="WE"?"selected":""} value="WE">West</option>
</select>

How to set a specific value at the end of struts 2 select element?

Here is an example, with set the value Please_select_a_value at the beginning of the rendered select list:
<s:select id="myGuiId" name="myGuiName" headerKey= "0" headerValue="Please_select_a_value" list="mySourceList" />
This generated the following HTML code:
<select name="myGuiName" id="myGuiId">
<option value="0">Please_select_a_value</option>
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
</select>
Is there a way, to set the value Please_select_a_value at the end of the rendered select list (if I open select list in GUI) without using some tricks in the correspondent Struts action class? Something like this in HTML code:
<select name="myGuiName" id="myGuiId">
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
<option value="21">Please_select_a_value</option>
</select>

How to verify the selected option in a dropdown in IE

I'm having some trouble verifying that an option is selected in a dropdown. In my application, I have a dropdown with 10 options. If I select option 5, I want to verify that it was actually selected.
Using Firefox and Chrome, its very simple. Below is the HTML for my dropdown and the options.
<select class="Test-field-ddlist" runat="server" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" name="ctl00$cphMainContent$ctl17">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected="selected">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
To verify the selected option, I created an object using the CSS selector. I then verified that the text is equal to 5.
public static final String ManagerPage_DropdownSelection = "css=.Test-field-ddlist option[selected]";
String actualtext = driver.getSingleElement(ManagerPage_DropdownSelection).getText();
Assert.assertEquals(actualtext, "5");
The problem I'm having with IE is that the HTML is different. When I inspect the element, the HTML looks like this. There is nothing that says 5 is selected
<SELECT name=ctl00$cphMainContent$ctl17 class=Test-field-ddlist onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" runat="server" sizcache="0" sizset="0">
<OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION>
<OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION>
<OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION>
<OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION>
<OPTION value=10>10</OPTION>
It looks like the "selected" text is hidden. If I copy and past the HTML into a text editor, I can then see the text "selected". Unlike Firefox and Chrome, there is not an attribute of selected. Instead, selected text is added to the value attribute.
<SELECT name=ctl00$cphMainContent$ctl17 class=Test-field-ddlist onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" runat="server" sizcache="0" sizset="0">
<OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION>
<OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=5 selected>5</OPTION>
<OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION>
<OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION>
<OPTION value=10>10</OPTION>
Can I create a selector that will find the selected dropdown option in all browsers? Or is there a better way to verify the selected value of a dropdown? I have tried the below method. It seems to work, but takes a very long time to complete (about 5 minutes).
protected void verifyDropDownSelection(String selector, String expectedvalue) {
List<String> listA = new ArrayList<String>();
listA.add(expectedvalue);
List<String> listB = new ArrayList<String>();
List<Element> DDSelected = driver.getSingleElement(selector).useAsDropdown().getAllSelectedOptions();
for(Element selectedoption : DDSelected) {
String actualtext = selectedoption.getText();
listB.add(actualtext);
}
log.info("INFO: Verifying the selected option in the dropdown");
Assert.assertEquals(listB, listA);
log.info("PASS: "+expectedvalue+" was the selected option in the dropdown");
}
I would let the Select class handle the case:
WebElement select = driver.findElement(By.name("ctl00$cphMainContent$ctl17"));
Select dropDown = new Select(select);
String selected = dropDown.getFirstSelectedOption().getText();
Assert.assertEquals(selected, "5");

Selenium WebDriver_Dropdown listed Element not able to select

Hello im not able to find following dropdown element. Here is HTML code:
<div class="FormDetails"><br>
<ul class="lsn"><br>
<li><br>
<span id="errfileName"></span><br>
</li><br>
<li style="display: block;"><br>
<div class="LeftSection-Form" style="margin-bottom: 15px;"><br>
<div class="FormLabel"><br>
<Community<br>
<span class="FormDetails_required"> Required</span><br>
</div><br>
<div class="FormValue"><br>
<select id="Fact-Communities-LIVE" class="ToolTipPopup Sel1355" tooltip-title=<p>"Community "</p> onkeydown="return preventBackspace(event);" <br>onchange="dropDownValue(this);" name="Fact.Communities.LIVE" emptyoption="-Please select-"><br>
<option value="TxnyD.PleaseSelect.1.1">-Please select-</option><br>
<option value="TxnyD.Communities.1.1">UVDB</option><br>
<option value="TxnyD.Communities.2.1">THQS</option><br>
<option value="TxnyD.Communities.3.1">Master</option><br>
<option value="TxnyD.Communities.4.1">Connexio</option><br>
<option value="TxnyD.Communities.5.1">SL</option><br>
<option value="TxnyD.Communities.6.1">OG</option><br>
<option value="TxnyD.Communities.7.1">UT</option><br>
<option value="TxnyD.Communities.8.1">TR</option><br>
<option value="TxnyD.Communities.9.1">FGW</option><br>
<option value="TxnyD.Communities.10.1">E.ON TSMS</option><br>
<option value="TxnyD.Communities.11.1">Vattenfall TSMS</option><br>
<option value="TxnyD.Communities.12.1">Delivery1</option><br>
<option value="TxnyD.Communities.13.1">Test community</option><br>
<option value="TxnyD.Communities.14.1">Automotive</option><br>
<option value="TxnyD.Communities.15.1">SHELL SUPPLIER QUALIFICATION SYSTEM</option> <br>
<option value="TxnyD.Communities.17.1">Nestle</option><br>
<option value="TxnyD.Communities.18.1">BuildingConfidence</option><br>
</select><br>
</div><br>
</div><br>
i tried following script to find these element:
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']")).click();
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]/text()='SHELL SUPPLIER QUALIFICATION SYSTEM'")).click();
tried these one as well:
//driver.findElement(By.id("Fact-Communities-LIVE")).sendKeys("SHELL SUPPLIER QUALIFICATION SYSTEM");
//Thread.sleep(1000);
//new Select(driver.findElement(By.id("Fact-Communities-LIVE"))).selectByVisibleText("SHELL SUPPLIER QUALIFICATION SYSTEM");
& these one as well
//WebElement dropDown = driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']"));
//List<WebElement> options = dropDown.findElements(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]/text()"));
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]")).click();
but seem to be something going wrong ..please any one guide me for these.
Try below code
WebElelment dropDown = driver.findElement(By.id("Fact-Communities-LIVE"));
new Select(dropDown).selectByVisibleText("SHELL SUPPLIER QUALIFICATION SYSTEM");
or
new Select(dropDown).selectByValue()("TxnyD.Communities.15.1");
Use the Select class:
Select select = new Select(driver.findElement(By.Id("Fact-Communities-LIVE");
and then use the option you want to select the options, by value, by text, by index etc.
Also check all the tags on the html if all of them are properly open and closed

Categories