Automating Combo Box (drop down + checkbox) using Selenium - java

I am trying to automate the drop down in the website Naukri.com. That drop down consists of multi select check-boxes. How can we automate it using Selenium Web driver?
The structure of the drop list is:
<div class="DDwrap">
<ul class="DDsearch">
<li class="tagit" data-id="tg_indCja_a8_A">
<span class="tagTxt">Accounting , Finance</span>
<span class="dCross"></span>
</li>
<li class="frst" style="float: left;">
<input id="cjaInd" class="srchTxt" type="text" placeholder="" name="" autocomplete="off" style="width: 30px;">
<input id="hid_indCja" type="hidden" name="indType" value="["8"]">
</li>
</ul>
</div>
Can anyone help me regarding this?

Check out the code below, It navigates to the concerned form, opens the dropdown of "Industry" and selects two checkboxes: 'Accounting , Finance' and 'Government , Defence':
WebDriver driver = new FirefoxDriver(); //Opening firefox instance
driver.manage().window().maximize(); //maximizing window
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds
driver.get("http://www.naukri.com/");
//Since there are two windows popping up, hence switching and closing the unnecessary window.
Set<String> windows = driver.getWindowHandles();
Iterator iter = windows.iterator();
String parentWindow = iter.next().toString();
String childWindow = iter.next().toString();
driver.switchTo().window(childWindow);
driver.close();
driver.switchTo().window(parentWindow);
//Hovering over "Jobs"
Actions act = new Actions(driver);
WebElement jobs = driver.findElement(By.xpath("//ul[#class='midSec menu']//div[.='Jobs']"));
act.moveToElement(jobs).build().perform();
//Clicking on "Advance Search"
WebElement Adv_search = driver.findElement(By.xpath("//ul[#class='midSec menu']/li[1]//a[.='Advanced Search']"));
act.moveToElement(Adv_search).click().perform();
//Clicking on the industry dropdown
driver.findElement(By.xpath("//div[#class='DDinputWrap']/input[contains(#placeholder,'Select the industry')]")).click();
//Selecting the checkbox containing text as "Accounting"
driver.findElement(By.xpath("//ul[#class='ChkboxEnb']//a[contains(text(),'Accounting')]")).click();
//Selecting the checkbox containing text as 'Government'
driver.findElement(By.xpath("//ul[#class='ChkboxEnb']//a[contains(text(),'Government')]")).click();

Related

Select menu element with Selenium

I would like to select an element of a dropdown menu using Selenium in a Java application.
I tried several times using Select, List < WebElement > etc... with no success.
The html of the webpage is this:
<div class="margin-top_2">
<span class="right_column_2">
<div id="f1:idSelectTipoDoc" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="vertical-align: middle; width: 198px;">
<div class="ui-helper-hidden-accessible">
<select id="f1:idSelectTipoDoc_input" name="f1:idSelectTipoDoc_input" style="vertical-align:middle">
<option value="">Select a fruit</option><option value="A">Apple</option>
<option value="T">Tangerine</option></select>
</div>
<input type="text" name="f1:idSelectTipoDoc_editableInput" class="ui-selectonemenu-label ui-inputfield ui-corner-all" tabindex="-1" style="cursor: pointer; width: 182px;">
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right"><span class="ui-icon ui-icon-triangle-1-s"></span></div></div></span>
</div>
This was my last try but the element in dropdown menu was not selected:
//open the dropdown menu
WebElement tipo1 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"f1:idSelectTipoDoc\"]/div[2]")));
tipo1.click();
// select the Apple line
WebElement tipo2 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"f1:idSelectTipoDoc\"]")));
Select elem = new Select(tipo2);
elem.selectByVisibleText("Apple");
Anyone know why it is not working? Thanks
your locator is wrong, you are using locator for div element and not select. use:
WebElement tipo2 = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[#id=\"f1:idSelectTipoDoc_input\"]")));
Select elem = new Select(tipo2);
elem.selectByVisibleText("Apple");

Click on submenu using selenium

I want to click on the submenu for the HTML code.
<div class="appDiv">
<div class="hreftheme_AppLinks_sub_links_Li_Megamenu">
<a onclick="appSubmit('/ultimatixPortalWeb/portlets/applications/redirect.jsp?PARAM=236','236','_blank')" href="#"> Timesheet Entry </a>
<span id="addTofavImage_236" class="favImage" onclick="" title=""> </span>
</div>
</div>
And i have tried the below possibilities but none of them worked out
1)
action.moveToElement(driver.findElement(By.className("appDiv"))).build().perform();
driver.findElement(By.className("hreftheme_AppLinks_sub_links_Li_Megamenu")).click();
2)
action.moveToElement(driver.findElement(By.linkText(Link name))).click().build().perform();
driver.findElement(By.linkText(str)).click();
3)
// JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", webelement for link );
4)
driver.findElement(By.xpath(".//*[#id='parentGroup_26']/div[1]/table/tbody/tr/td[1]/div/div[12]/div/a")).click();
and also with absolute xpath but none of them worked out.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("My Sub Menu"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.cssSelector("My Sub Menu Item")).click();

How to fetch dropdown value from List in selenium Webdriver by using java

How to consider list value as a dropdown value?
How to select Edit Drop down value from List...
<div class="nice-select demoBasic" tabindex="0">
<span class="current">Please select</span>
<ul class="list">
<li class="option" data-value="1">Edit</li>
<li class="option" data-value="2">Delete</li>
</ul>
</div>
I got it solution ...
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div/ul/li[1]")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
You should try using By.cssSelector() to get all options in list as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//First click on select div
WebElement select = wait.until(ExepectedConditions.elementToBeClickable(By.cssSelector("div.nice-select.demoBasic")));
select.click();
//Not get all nested options
List<WebElement> options = wait.until(ExepectedConditions.presenceOfNestedElementsLocatedBy(select, By.cssSelector("ul.list li.option")));
//Now iterate to fetch all options and select edit option
for(WebElement option : options)
{
if(option.getText().equals("Edit"))
{
option.click();
}
}
Or you can select an option on the basis of text in one line using By.xpath() as below :-
wait.until(ExepectedConditions.elementToBeClickable(By.xpath(".//li[text() = 'Edit']"))).click();

Selenium Webdriver javascript - pop up is getting open 4 times on every dropdown click

I am new to selenium. The scenario is that pop up is getting open when I click on popupid, there are 4 dropdowns in the pop up where i need to select the values from each dropdown but while selecting values from each dropdown new pop up window opens which overrides the existing one. Due to which also facing issue in selecting the correct values.
Selenium code:
WebDriverWait waitFortab = new WebDriverWait(driver, 15);
waitFortab.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".node.settings.asettings.bookmgmt.undefined.ready")));
WebElement el5 = driver.findElement(By.cssSelector(".bookrouting")); // opens the page
js.executeScript("arguments[0].click();", el5);
WebElement el6 = driver.findElement(By.cssSelector(".ruleEnControls .fr.bookroute.addcategory")); // opens up popup
el6.click();
WebDriverWait waitForcategory = new WebDriverWait(driver, 15);
waitForcategory.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".popup.wrapper.bookroute_save.dffPopup")));
WebElement el8 = driver.findElement(By.cssSelector(".tag.dropdown.product_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el8);
WebElement el9 = driver.findElement(By.xpath("//*[text()='Vanilla (V)']")); // selects value from first pop up
js.executeScript("arguments[0].click();", el9);
WebElement el10 = driver.findElement(By.cssSelector(".tag.dropdown.currency_pair_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el10);
WebElement el11 = driver.findElement(By.xpath("//*[text()='AUD/CAD']")); // selects value from second pop up
js.executeScript("arguments[0].click();", el11);
WebElement el12 = driver.findElement(By.cssSelector(".tag.dropdown.currency_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el12);
WebElement el13 = driver.findElement(By.xpath("//*[text()='AUD']")); // selects value from third pop up
js.executeScript("arguments[0].click();", el13);
HTML Snippet :
<ul class="dropdown_menu dn">
<li class="option selectAllBR" data-value="" shortcut="" data-display="Select All" title="Select All">
<div class="tag fl checkBoxClear ddselect checkbox r0 c0" irow="0" icol="0" tabindex="0" fieldtype="ddselect" tagtype="checkbox"><input type="hidden" value="0" class="toggleValue value"></div>
<span class="oLayCheck"></span>Select All</li>
<li class="option" data-value="euvanilla" shortcut="VANILLA (V)" data-display="Vanilla (V)" title="Vanilla (V)">
<div class="tag fl checkBoxClear ddselect checkbox r0 c0" irow="0" icol="0" tabindex="0" fieldtype="ddselect" tagtype="checkbox"><input type="hidden" value="0" class="toggleValue value"></div>
<span class="oLayCheck"></span>Vanilla (V)</li>
<!-- there are several such <li> elements in each dropdown -->
</ul>
I bet you're running in to some timing issues with the popups being present, but not yet ready on the DOM--so the clicks are happening on the previous elements.
Try something like this:
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement el8 = driver.findElement(By.cssSelector(".tag.dropdown.product_brm .downArrow.closed"));
wait.until(ExpexctedConditions.elementIsClickable(el8);
js.executeScript("arguments[0].click();", el8);
WebElement el9 = driver.findElement(By.xpath("//*[text()='Vanilla (V)']")); // selects value from first pop up
wait.until(ExpexctedConditions.elementIsClickable(el9);
js.executeScript("arguments[0].click();", el9);
A couple things:
You don't need a new wait for different things. Just reuse existing one.
Note that the pattern is
Find the element
Wait for element to be clickable
Click the element.
Also, not sure why you're using the JavaScriptExecutor. You could simply do "el9.click();" unless there's something goofy with JavaScript on your pop ups.
HTH

Exception in thread "main" org.openqa.selenium.InvalidSelectorException

Trying to select a Menulink i tried through various options like through Linktext, Xpath but it shows cannot find such element.
The HTML expression is:
<div class="bd">
<ul class="first-of-type">
<li class="yuimenubaritem clsmenubaritem yuimenubarItem-has submenu first-of-type" id="MenubarItem_new1428659607679" groupindex="0" index="0"> New</li>
</ul>
</div>
I tired all my possible options which are
1)
WebElement menulink = wd.findElement(By.xpath("//* [#id='MenuBarItem_new1428659607679']"));
Actions action= new Actions(wd);
action.moveToElement(menulink).build().perform();
Thread.sleep(5000L);
2)
WebElement menulink=wd.findElement(By.linkText("New"));`
3)
WebElement menulink=wd.findElement(By.xpath("//ul[contains(#class,'first of type')]and //li[contains(#id,'MenuBarItem_new1428643471800') ] "));
menulink.findElement(By.tagName("a")).click();
menulink.click();
driver.findElement(By.cssSelector("div.bd li.submenu a")).click();

Categories