How to select items from dropdown list by selenium - java

In my testing UI, I need to automate a case as this in selenium java.
1. To click an icon, so that the pop-up list will be shown up,
2. then to select an item from the pop-up list.
But I am clueless how this can be done from following html code. This is the code after the icon has been clicked.
<ul class="pull-right header-helpers">
<li class="helpers-user hide-min-width"> ... </li>
<li class="helpers-help hide-min-width"> ... </li>
<li class="dropdown user-dropdown open">
<div class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<div>...</div>
</div>
<div class="dropdown-backdrop"></div>
<ul class="dropdown-menu dropdown-menu-right">
<li class="dropdown-menu-item">
<a action = "Option-A"/>
</li>
<li class="driver" role="separator"></li>
<li class="dropdown-menu-item">
<a action = "Option-B"/>
</li>
<li class="driver" role="separator"></li>
</ul>
</ul>
If icon hasn't been clicked, line 4 would be as this:
<li class="dropdown user-dropdown">
Thanks for the help.

Wait for the dropdown value you want to select and click it. This is to select the option A in the dropdown. You can replace option value with params and handle dynamically to select required option.
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement dropdown= wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".dropdown-menu a[action='Option-A']")));
dropdown.click();

Related

Cannot save value in span control using Selenium Webdriver and Java

I'm using Selenium Webdriver + Java to do automation tests and stuck with the problem of setting a value in span control.
I have a drop-down with list of clients, the 1st client is selected by default - 'Harry', but I need to change the client to be other than default, e.g. to 'Simon'.
I tried to search here and came with the following code:
Webelement element = driver.findElement(By.xpath("//*[#id=\"SelectedClientDirectorID_chosen\"]/a/span"));
((JavascriptExecutor)driver).executeScript("arguments[0].innerText = 'Simon'", element);
It seems to be fine, the element is found, it's innerText property is changed to 'Simon' while script is being executed.
But as soon as I click 'save' button on the page, the record is saved with the 1st value - 'Harry'.
I could not find the answer on why the new value is not saved.
Html looks like:
<div class="chosen-container chosen-container-single" style="width: 100%;" title="" id="SelectedClientDirectorID_chosen">
<a class="chosen-single" tabindex="-1">
<span class="">Harry</span>
<div><b></b></div>
</a>
<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off">
</div>
<ul class="chosen-results">
<li class="active-result result-selected" style="" data-option-array-index="0">Harry</li>
<li class="active-result result-selected" style="" data-option-array-index="1">Simon</li>
<li class="active-result result-selected" style="" data-option-array-index="2">Robin</li>
<li class="active-result" style="" data-option-array-index="3">Brian</li>
<li class="active-result" style="" data-option-array-index="4">Rupert</li>
<li class="active-result" style="" data-option-array-index="5">Greg</li>
</ul>
</div>
</div>
Appreciate any clue.
Changing innerText value might not bring expected result in your case. You can try to click on drop-down and select required option just like real user do:
driver.findElement(By.xpath("xpathForDropDownButton")).click();
driver.findElement(By.xpath("//li[text()='Simon']")).click();
You might also need to wait some time until required elements become clickable:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpathForDropDownButton"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Simon']"))).click();
Use the following code for the same. Instead of setting innerText, you need to select value from dropdown.
WebElement element = driver.findElement(By.xpath("//ul[#class='chosen-results']/li[normalize-space()='Simon']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

How to Click the Tab

I want to click the "Get" tab illustrated in this screenshot. But unable to click. Can someone please help me?
HTML is:
<div id="schemeSlabTab" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top">
Buy
</li>
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
Get
</li>
</ul>
You can try this:
WebElement element = driver.findElement(By.xpath("//a[contains(#href,'#Init_Get_Grid')]")).findElement(By.linkText("Get"));
element.click();

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

How to print the all li items text of dropdown using Selenium WebDriver?

How I print the all li items text using selenium webdriver.
Here I want to some type of single line code that print all li items text like
Jackets,
protective etc. May by using for loop
<div class="dropdown change_right">
<ul>
<li>
<a class="" title="Jackets" href="/collections/jackets">Jackets</a>
</li>
<li>
<a title="Jackets protective" href="/collections/jackets/jackets-protective" class="sub-link"> protective</a>
</li>
<li>
<a title="casual leather" href="/collections/jackets/casual-leather" class="sub-link">casual leather</a>
</li>
<li>
<a class="" title="T-shirts & shirts " href="/collections/t-shirts-shirts">T-shirts & shirts </a>
</li>
<li>
<a title="shirts" href="/collections/t-shirts-shirts/shirts" class="sub-link">shirts</a>
</li>
<li>
<a title="crew-neck" href="/collections/t-shirts-shirts/crew-neck" class="sub-link">crew-neck</a>
</li>
<li>
<a title="polo tshirts" href="/collections/t-shirts-shirts/polo-tshirts" class="sub-link">polo tshirts</a>
</li>
<li>
<a class="" title="Trousers" href="/collections/trousers">Trousers</a>
</li>
<li>
<a title="Trousers Protective" href="/collections/trousers/trousers-protective" class="sub-link"> Protective</a>
</li>
</ul>
Can be done with a simple findElements(). With the help of xpath to grab the collection of elements and a simple iterate through the collection and print the value with the help of getText()which returns the string value of the element.
List<WebElement> allText = driver.findElements(By.xpath("//*[contains(#class,'dropdown')]//a"));
for ( WebElement element: allText) {
System.out.println(element.getText());
}
Note: untested code. You may need to make sure click and open the list happens before executing this code and some wait might also be necessary as well

selenium webdriver click link in the tag

<div id="sbGlobalNav" class="">
<div id="sbGlobalNavContent">
<div id="sbGlobalNavRightContent">...</div>
<div id="sbGlobalNavLeftContent">
<ul id="globalNavMenu">
<li class="globalNavSeparator firstSeparator">..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li class="globalNavDropdown">
<span>Answer</span>
<ul class="globalNavDropdownContainer" style="display: none;">
<li>..</li>
<li>Daily Polls</li>
<li>..</li>
</ul>
</li>
<li class="globalNavDropdown">..</li>
<li class="globalNavSeparator">..</li>
<li class="globalNavDropdown" id="globalNavRewards">..</li>
<div class="clear"></div>
In this I need to find and click Daily Poll link using Selenium webdriver(java) but I was unable to do it.
What I did is:
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.xpath("//div[#id='sbGlobalNav']//span[text()='Answer']"))).build().perform();
driver.findElement(By.linkText("Daily Polls")).click();
Check it, whether you have to so something to trigger that element out. Javascript maybe, by mouse over ?
Try this:
driver.findElement(By.xpath("//*[text()='Daily Polls']")).click();
It might work.

Categories