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

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

Related

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

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

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

WebDriver can't select from JQuery dropdown

I have a web form having JQuery dropdowns. The particular field holds date of birth. The source for the field is:
<div class="tooltipGroup" style="z-index:19;">
<div class="day">
<div class="jqTransformSelectWrapper" style="z-index: 19;">
<div>
<ul style="width: 100%; display: block; visibility: visible;">
<li class="optHeading">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<a index="6" href="#">6</a>
</li>
<li class="undefined">
<a index="31" href="#">31</a>
</li>
That's the code trying to get all of the elements and put them in a HashMap:
public void selectDob(int dob) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
dropdownValues.get(dob).click();
}
The code works just fine with one exception: it can't get the values of all fields, just the first visible when the dropdown is being opened.
1 2 3 4 5
The question is how to get the values of the other fields?
I would like to thank you guys for your help (especially HemChe).
It turned out that this is a bug in the latest FirefoxDriver version 2.32. The same code worked just fine with ChromeDriver and I've got all of the drropdown values. Downgrading the selenium version to 2.31 solved that problem and the code works with both drivers!
I'll register a bug on the Selenium bug tracker!
That's the final version of my code:
public void selectFromDropdown(String option) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText().toString());
}
dropdownValues.get(option).click();
}
Cheers!
Try the below code and check if it is working.
WebElement w = driver.findElement(By.id("aWrapper_dob_day"));
w.click();
WebElement dobFieldDropdown = driver.findElements(By.className("undefined"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
You can't locate invisible elements with Web Driver, you need to use JavaScript in order to obtain them. So try something like
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("get them here by class name");

Categories