Unable to select element in Selenium WebDriver from list box - java

I am new to Selenium Web Driver and unable to select element in the list box and getting below error:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"ext-element-13"}
WebElement item = launch.driver.findElement(By.id("ext-element-13"));
Select page_dropdown = new Select(item);
for(int i=0; i<page_dropdown.getOptions().size(); i++)
{
page_dropdown.selectByIndex(i);
System.out.println("Page drop down having values " + page_dropdown.getFirstSelectedOption().getText());
}
<div id="boundlist-1123" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-border-box" tabindex="-1" style="width: 215px; right: auto; left: 574px; top: 140px; z-index: 19000; height: 300px;" data-componentid="boundlist-1123">
<div id="boundlist-1123-listWrap" class="x-boundlist-list-ct x-unselectable" data-ref="listWrap" style="overflow: auto; height: 299px;">
<ul id="boundlist-1123-listEl" class="x-list-plain" aria-disabled="false" aria-hidden="false" role="listbox" data-ref="listEl">
<li id="ext-element-13" class="x-boundlist-item x-boundlist-selected" unselectable="on" role="option" tabindex="-1" data-recordindex="0" data-recordid="3" data-boundview="boundlist-1123">ABC Program : Summary</li>
<li class="x-boundlist-item" unselectable="on" role="option" tabindex="-1" data-recordindex="1" data-recordid="4" data-boundview="boundlist-1123">ABC-001: Poor Control</li>
<li class="x-boundlist-item" unselectable="on" role="option" tabindex="-1" data-recordindex="2" data-recordid="5" data-boundview="boundlist-1123">ABC-002: Low Density </li>

As per provided HTML code, i am expecting it is not Select drop down. so select command may not work here..
secondly if i am correct
<ul id="boundlist-1123-listEl" class="x-list-plain" aria-disabled="false" aria-hidden="false" role="listbox" data-ref="listEl">
is location of your drop down and below one is one of option correct?
<li id="ext-element-13" class="x-boundlist-item x-boundlist-selected" unselectable="on" role="option" tabindex="-1" data-recordindex="0" data-recordid="3" data-boundview="boundlist-1123">ABC Program : Summary</li>
here you may need to use sendkeys
driver.findElement(By.xpath("//*[#role='listbox']")).sendKeys("ABC Program : Summary");
you can also use xpath=//*[#data-ref='listEl'] in above code if there are more listbox elements in page
if above one does not work, just try by directly clicking on required option
driver.findElement(By.xpath("//*[contains(text(),'ABC Program : Summary')]")).click();
if even above one does not work, then you need to click on ul and then required option.
there may be change in locators, if they are not working let me know, will try for another xpath

Related

Selenium: Cant click first item in dropdown

In my web page I want to click on the txtbox which will drop down a list, and then click on the first option. I can't use the selector option since the it isn't a select type. So I am using the Action in selenium to move my mouse to click. When in the web page if I click over the textbox the dropdown will list the items. But in my selenium code it can find the txtbox element, but when I click it, it can't element in the list. I don't think the list appears when I click on the txtbox. Heres my current code:
Webelement txtbox = driver.findElement(By.xpath("//*[#id=\"lob\"]"));
Webelement inpt =driver.findElement
(By.xpath("/html/body/div[2]/div/div/div/div[2]/div/div/div[1]"));
mouseHoverAction(txtbox);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(inpt));
Utils.mouseHoverAction(inpt);
public static void mouseHoverAction(WebElement mainElement){
Actions action = new Actions(driver);
action.moveToElement(mainElement).perform();
action.click();
action.perform();
Log.info("Click action is performed on the selected Product Type");
}
In my Log file it never says that the click action was performed but it does say the textbox was found.
Here is the HTML before I click on textbox:
<div class="ant-select margin___3sLIj ant-select-lg ant-select-single
ant-select-show-arrow" name="lineOfBusiness" style="width: 100%;">
<div class="ant-select-selector">
<span class="ant-select-selection-search">
<input id="lob" autocomplete="off" class="ant-select-selection-search-input"
role="combobox" aria-haspopup="listbox" aria-owns="lob_list" aria-autocomplete="list"
aria-controls="lob_list" aria-activedescendant="lob_list_0"
readonly="" unselectable="on" value="" style="opacity: 0;" aria-expanded="false">
</span>
<span class="ant-select-selection-placeholder">Please select one
</span>
</div>
>
Here is HTML after I click drop down and insepct first element in list "Business Enablement"
<div class="ant-select-dropdown ant-select-dropdown-placement-bottomLeft
ant-select-dropdown-hidden" style="min-width: 205px; width: 205px;
left: 182.984px; top: 614.984px;">
<div>
<div role="listbox" id="lob_list" style="height: 0px; width: 0px; overflow: hidden;">
<div aria-label="Business Enablement" role="option"
id="lob_list_0" aria-selected="false">Business Enablement</div>
<div aria-label="Capital Markets" role="option"
id="lob_list_1" aria-selected="false">Capital Markets</div>
</div>
<div class="" style="height: 256px; overflow-y: auto; overflow-anchor: none;">
<div style="height: 480px; position: relative; overflow: hidden;">
<div class="" style="display: flex; flex-direction: column; transform: translateY(0px);
position: absolute; left: 0px; right: 0px; top: 0px;"><div aria-selected="false"
class="ant-select-item ant-select-item-option ant-select-item-option-active">
<div class="ant-select-item-option-content">Business Enablement
</div>
<span class="ant-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"></span>
</div>
<div class="ant-select-item-option-content">Capital Markets
</div>
<span class="ant-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"></span>
</div>
<div class="ant-select-item-option-content">Caribbean Banking
</div>
<span class="ant-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"></span>
</div>
<div class="ant-select-item-option-content">Cloud
</div>
<span class="ant-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"></span>
</div>
</div>
</div>
</div>
</div>
I need help selecting "Business Enablment"
Extra Pics
This is how you can attempt to try it -
Click the dropdown
Right click on the first element of the dropdown, inspect . After the developer option is open, right click on that element, copy full xpath. Now you will need to tweak the xpath to capture everything from the list. Sometimes you can just use //li and get content of the list or sometimes you will have to add * to either table, tr depending on the xpath
Once you get the xpath to capture the list, store it in List
List brand = driver.findElements(By.xpath("use your custom xpath"));
Iterate through the list and store the text
Search for the value you want to click
Click the element when you find
This is one of the method I have written for my project
public void selectBrand(String Brand) {
List<WebElement> brand = driver.findElements(By.xpath("//li"));// this will store all elements from brands dropdown, again you will need to use your custom xpath
for (WebElement brandlist : brand)
{
String brandName = brandlist.getText();// store all text from dropdown
if (brandName.length() > 1) {
if (brandName.contains(Brand))// search for the value to click
{
brandlist.click();// click
}
}
}
}

List WebElement always selecting first element from the drop-down but i want to select 4th element

My list has approximate 190 items and items also getting traced one by one in for loop. I applied the condition to a specific text. however, debugger entering into condition but selecting always the first element from the drop-down. In mentioned code country "America" present on the 4th index of my drop-down.
List<WebElement> options=driver.findElements(By.xpath("//*[#id='Countryitems_popup']/div[1]/ul//li/span"));
for(int i=0;i<options.size();i++)
{
WebElement select=options.get(i);
String innerhtml=select.getAttribute("innerHTML");
if(innerhtml.contentEquals("America"))
{
select.click();
break;
}
}
HTML is mentioned below:
<div id="Countryitems_popup" class="e-scroll e-js e-wid" tabindex="" style="height: auto; display: block;">
<div class="h-con" style="height: 150px; width: 158.891px;">
<ul class="H-Kl" role="listbox">
<li data-value="001" id="004" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 1</span>
</li>
<li data-value="676" id="006" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 2</span>
</li>
<li data-value="765" id="009" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 3</span>
</li>
<li data-value="0067" id="065" role="option" unselectable="on" class="">
<span class=" e-ddltxt">America</span>
</li>
</ul>
</div>
</div>
Use for each loop for more readability. Try with this code :
List<WebElement> options= driver.findElements(By.xpath("//[#id='Countryitems_popup']/div[1]/ul//li/span"));
for(WebElement option : options) {
if(option.getText().trim().equals("America")) {
option.click();
break;
}
}
This should work, provided the xpath should be correct.
Without seeing the page to test it, it's hard to know exactly what the issue is but if it's like other pages I've seen you will need to:
Click the "dropdown" to open it (and make the options visible)
Wait for the desired option to be clickable (indicating the list is open and visible)
Click the desired option
My recommendation is to put this all in a method called selectCountry() (or something similar) and pass in the desired country name.
public void selectCountry(String countryName)
{
// I'm assuming this is the container for the dropdown. Clicking it should open the dropdown
driver.findElement(By.id("Countryitems_popup")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='Countryitems_popup']//span[.='" + countryName + "']))).click();
}
You can achieve it with selector itself without loop. The xpath to find the list element with text 'America' will be as,
driver.findElement(By.xpath("//[#id='Countryitems_popup']/div[1]/ul//li[conatins(text(), 'America')]")).click()

selenium webdriver-Xpath is not working for list

i need to select the data from Excel file . Here i am using keyword driven framework. This 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>
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.
TRY #1
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();
TRY#2
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[#class='ms-list']/li/span[contains(text(),'"+testData+"')]"))).click();
If i have 10 data in list and i am reading them from my Excel file it above codes only select 3 or 4 and then gave me exception.
Then i tried with this one:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='ms-addContract']/div[1]/input"))).sendKeys(testData);
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.SPACE);
Thread.sleep(2000);
//Clear the input text value.
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).clear();
where it is sending data to filter textbox and selecting the popped up input. But it is taking time and if i don't put delay giving me timeout Exception.
Please help.

How to select from the drop down using Selenium

Please see the code below where I need to click on dropdown so that list is displayed
<span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">
<span class="k-dropdown-wrap k-state-default k-state-hover k-state-focused" unselectable="on">
<span class="k-input" unselectable="on">Is equal to</span>
<span class="k-select" unselectable="on">
<span class="k-icon k-i-arrow-s" unselectable="on">select</span>
</span>
</span>
driver.FindElement(By.CssSelector(".k-widget k-dropdown k-header")).click()
I think you again have to try by clicking using JavascriptExecutor
WebElement element1 = driver.findElement(By.xpath("//span[#class='k-widget k-dropdown k-header' and #role='listbox']"));
JavascriptExecutor executor1 = (JavascriptExecutor) oBrowser;
executor1.executeScript("arguments[0].click();", element1);
If above Xpath didn't work then try below:-
//span[#class='k-widget k-dropdown k-header']
Hope it will help you :)
I ran across this exact issue the other day. The problem is the Kendo UI <span> element you are referencing uses a <ul> element to get it's innertext but it's the <ul> list that handles the option change/selection. You won't be able to select an option from the pop-up/dropdown you see when clicking the <span>, you have to click the <li> element that holds the text options referenced by the span.
In your example, somewhere else in the HTML document you'll find a <ul> list similar to...
<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" aria-live="off" data-role="staticlist" role="listbox">
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-offset-index="0">is equal to</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">another option</li>
</ul>
Using Selenium 3, if you wanted to select "another option":
driver.findElement(By.xpath("//li[text()='another option']")).click();
You should see your <span> innertext change to 'another option' and you can continue on your testing journey.

FluentWait for dynamically created ListBox

I have a listbox(droplist) item that is created dynamically when I click on a div/span element .. However, when I try to click on the listbox element after doing a fluent Wait , it throws me an error saying
Element is not clickable at point (741, 192). Other element would receive the click:"
If i try to use Thread.sleep(2000) then the code runs perfectly. Any ideas on how I can fix this issue and make it work in fluentWait. I have tried elementTobeClickable,elementSelectionStateToBe,presenceOfElementLocated methods in the ExpectedConditions class and they all throw the same error. I am using ChromeDriver for testing purposes .
My code and HTML snippet of the page is below
WebElement elt = driver.findElement(By.xpath("//*[#id='GroupTT']/span[2]/span/span[1]")); //Clicking on the span so that listbox will be activiated
Actions act = new Actions(driver);
act.moveToElement(elt).click().build().perform();
fWait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//ul[#id='Group_listbox']/li[2]")))); //Thread.sleep(2000) works fine here.
driver.findElement(By.xpath("//ul[#id='Group_listbox']/li[2]")).click();
HTML Snippet
<div >
<ul unselectable="on" id="Group_listbox" role="listbox" style="overflow: auto; height: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-index="0"> Group1 </li>
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-index="1"> Group2 </li>
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-index="1"> Group3 </li>
</ul>
</div>
You can try to change the XPath locator to find the element by its inner text.
fWait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[text() = 'Group2']"))));

Categories