Click on submenu using selenium - java

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

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");

Need help- Selenium webdriver- click on element from visible list is not working

I am sending defined data from excel file. I tried some code but they are not selecting all the data from excel file at some point of time code is giving me exception for WebElement not found.
Here is the HTML code:
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<div id="addDialog" class="hidden ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 30px; height: auto; max-height: 351.05px; overflow-y: auto;">
<div class="field-container">
<fieldset class="field-container">
<legend>Contracts:</legend>
<a class="select-all" href="#">Select All</a>
<a class="deselect-all" href="#">Deselect All</a>
<select id="addContract" class="searchable" multiple="multiple" style="position: absolute; left: -9999px;">
<option value="93370956">93370956</option>
<option value="93796167">93796167</option>
<option value="94203239">94203239</option>
</select>
<div id="ms-addContract" class="ms-container">
<div class="ms-selectable">
<input class="search-input" type="text" placeholder="filter" autocomplete="off"/>
<ul class="ms-list" tabindex="-1">
<li id="86355560-selectable" class="ms-elem-selectable">
<span>93370956</span>
</li>
<li id="202890296-selectable" class="ms-elem-selectable">
<span>93796167</span>
</li>
<li id="938848030-selectable" class="ms-elem-selectable">
<span>94203239</span>
</li>
</ul>
</div>
</div>
Need to select values from list.
Efforts done:
This effort for code worked but it selected only one value and then gave exception
WebDriverWait Wait=new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[#class='ms-list']/li/span")));
//now you can find element
List<WebElement>options=driver.findElements(By.xpath("//ul[#class='ms-list']/li/span[contains(text(),'"+testData+"')]"));
for (WebElement option: options) {
if(testData.equals(option.getText())) option.click();
}
Tried above code but it only selects one value !!
WebDriverWait Wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='ms-addContract']//descendant::div[#class='ms-selectable']/ul[#class='ms-list']]//span")));
List<WebElement> options = driver.findElements(By.xpath("//*[#id='ms-addContract']//descendant::div[#class='ms-selectable']/ul[#class='ms-list']]//span[contains(text(), '"+testData+"')]"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.click();--tried this xpath-no success
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='ms-addContract']/div[1]/input"))).click();
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).sendKeys(testData);
WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='ms-addContract']/div[1]/input"))).sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.SPACE);
//Clear the input text value.
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).clear();---worked but not satisfactory
Please need help to locate the element. I am using keyword driven framework reading values from excel file.
It sounds like you are trying to get all span elements that are contained within a ul element (although correct me if I am wrong). This is the code you are using:
List<WebElement>options = driver.findElements(By.xpath("//ul[#class='ms-list']/li/span[contains(text(),'"+testData+"')]"));
In your case, this XPath will only return 1 value. This is because you are trying to match the contents of each span with an input value "testData". If you want to get all span elements inside of the ul then use:
List<WebElement>options = driver.findElements(By.xpath("//ul[#class='ms-list']/li/span"));
This XPath will select accomplish selecting all span elements in the list. If you need to select only span elements containing text within your testData, then you can iterate over that list selecting the appropriate elements:
ArrayList<WebElement> optionsInTestData = new ArrayList<WebElement>();
for(WebElement element: options){
for(String data: testData){
if(element.getText() == data){
optionsInTestData.add(element);
}
}
}

Selenium Webdriver : How to navigate to child div and move foucs over specific div

Currently facing the issue in navigating to many div tags.
In my application, I have a grid which populates or load data in multiple div tags.
On mouseover, I have to click add comment option which gets loaded as lightbox popup.
Please guide me how to select add comment option.
<div ng-cell="">
<div class="inCell ng-scope">
<div class="cellDarkGray" ng2-class="{cellDarkGray: (row.getProperty('returnedRecordsNumber') != '0'),cellDarkGray: (row.getProperty('activityid') != '0'),cellDarkGray: (row.getProperty('returnedRecordsNumber') != '0')}">
<div class="cellOrange" ng-class="{cellRed: (row.getProperty('Impact') == '0'),cellOrange: (row.getProperty('activityid') != '0'), cellBlue: (row.getProperty('Impact') == '2')}"/>
<div class="cellWhite">
<div style="height:60px">
<div class="ng-hide" ng-show="(row.getProperty('commentFlag')== true) && (userRole =='ROLE_2') ">
<div class="ng-hide" ng-show="(row.getProperty('commentFlag')== true) && (userRole =='ROLE_1') ">
</div>
<div class="inCellData">
<div class="line1" ng-click="openDetails(row,'viewDetail')">
<span class="title1 ng-binding" ng-hide="row.getProperty('planned_typ')== 'NULL'">ORANGE PLANNED MAINTENANCE - </span>
<span class="title2 ng-binding" ng-hide="row.getProperty('deviceid')== 'NULL'"> BLCY810</span>
<!-- <span ng-hide="row.getProperty('myRouterName')== ''"> ||{{row.getProperty('myRouterName')}} ||</span> -->
</div>
<div class="line2">
<span class="title3"/>
<span class="ng-binding" ng-hide="row.getProperty('service_impact')== ''"> 1 - 5 MINS ||</span>
<span class="title3 ng-binding">09/JUN/2017 01:30:00 AM</span>
up to
<span class="title2 ng-binding">09/JUN/2017 03:30:00 AM</span>
||
<span class="ng-binding">EQUANT SHARED NETWORK</span>
</div>
<div class="line3">
</div>
<div>
<nav id="cbp-spmenu-s1" class="cbp-spmenu cbp-spmenu-vertical cbp-spmenu-left cbp-spmenu-open">
<a ng-click="openDetails(row,'viewDetail')">view details</a>
<div class="" ng-show="userRole =='ROLE_1'">
<a class="" ng-show="row.getProperty('internalFlag')=='Y'" ng-click="setUnsetInternal(row)">invisible to customer</a>
<a class="ng-hide" ng-show="row.getProperty('internalFlag')=='Z'" ng-click="row.getProperty('sourcesystem')=='Carrier Changes' && (row.getProperty('urgency')=='EMERGENCY' || row.getProperty('urgency')=='EXPEDITE') ? alertPastDataZEmergency() : alertPastDataZ() ">invisible to customer</a>
<a class="ng-hide" ng-show="row.getProperty('internalFlag')=='N'" ng-click="setUnsetInternal(row)">visible to customer</a>
<a class="ng-hide" ng-show="row.getProperty('internalFlag')=='X' || row.getProperty('internalFlag')=='H'" ng-click="alertPastDataX()">visible to customer</a>
</div>
<!-- Prod Consol Start -->
<a ng-show="userRole =='ROLE_1'" ng-click="openDetails(row, 'comment')">add a comment</a>
<!-- Prod Consol End -->
</nav>
</div>
</div>
</div>
First of all you have to move the mouse using Action class to the element so that Comment link will be visible. Then you have to perform action on that element.
You can do so like this -
WebElement elem = driver.findElement(By.xpath("//a[contains(text(), 'add a comment')]"));
Actions action = new Actions(driver);
action.moveToElement(elem).click().build().perform();
Hope this helps.
Do mouse hover to the parent element to make the comment visible. Below code might give you some idea.
Actions action = new Actions(driver);
action.moveToElement("//nav[#id='cbp-spmenu-s1']").perform();
//Now the comment button will be visible
driver.findElement(By.xpath("//a[contains(text(), 'add a comment')]")).click();
Hope this helps. Thanks.
I resolved this issue by using below code
WebElement we1 = driver.findElement(By.xpath("(//a[contains(text(),'add a comment')])[1]"));
JavascriptExecutor elem = (JavascriptExecutor)driver;
elem.executeScript("arguments[0].click();", we1);

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

Automating Combo Box (drop down + checkbox) using Selenium

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

Categories