I want to get all options from select button.
<select id="filterSel" name="filterSel" class="fixed-size" onchange="fnLoadAccountslegEnt(this.value); " onclick="closeDropDown();" size="1" style="top: 6.5px; margin: 0px; width: 58%; height: 30px; z-index: 9999; background: rgb(255, 255, 255);">
<option value=""></option>
<option value="aa" title="abcd">abcd</option>
<option value="bb" title="xyz">xyz</option>
<option value="More" style="color: blue;">More...</option>
</select>
3rd option is "More..." on pressing this more options load into the dropdown. i.e., more options are visible only now. I need to access all options using selenium WebDriver. Using the following code I am able to get only what is there already in the options tag to be printed on console...last option printed is "More...".
my code:
Select select=new Select(element_select);
List<WebElement> options = select.getOptions();
int i=1;
for(WebElement ele:options) {
if(str.contains("More")) {
Filter.FilterApplied().sendKeys(str);
Filter.FilterApplied().click();
}
str=ele.getText();
System.out.println("options are :"+str);
i++;
}
Error message:
FAILED: main org.openqa.selenium.WebDriverException: unknown error:
Element ... is not clickable at point
(262, 84). Other element would receive the click:
You could use a JSTL for loop to populate the dropdown dynamically.
1. Add an action to the
<option value="More" onclick="//goToSomeController" style="color: blue;"><%= //could be an array of options %></option>
and from there you can populate the size of the loop and the array with options. Hope that helps.
You can use the Select class to select the more option and load all the options
Select select = new Select(element_select);
List<WebElement> options = select.getOptions();
int size = options.size();
select.selectByValue("More");
while ((options = select.getOptions()).size() == size);
int i = 1;
for(WebElement ele : options)
{
str = ele.getText();
System.out.println("options are :"+str);
i++;
}
while ((options = select.getOptions()).size() == size); will wait for more options to load, use it this way only if you are certain there are more options coming, otherwise you will end up with infinite loop.
Related
I am trying to select an option from a drop-down that does not populate until the locator has been clicked. This my solution but it's not working.
List<WebElement> options = driver.findElements(By.cssSelector("mat-select"));
for (WebElement option : options) {
if (option.getAttribute("ng-reflect-value").contentEquals("50757")) {
Actions build = new Actions(driver);
build.moveToElement(option).click().build().perform();
}
}
HTML of the dropdown.
<div role="listbox"
tabindex="-1"
class="ng-tns-c114-22 ng-trigger ng-trigger-transformPanel mat-select-panel mat-primary"
id="mat-select-0-panel"
aria-multiselectable="false"
aria-labelledby="mat-form-field-label-27"
style="transform-origin: 50% 22px 0px; font-size: 15px; opacity: 1; min-width: calc(100% + 32px); transform: scaleY(1);">
<mat-option
_ngcontent-tqo-c274=""
role="option"
class="mat-option mat-focus-indicator mat-active ng-tns-c114-22 ng-star-inserted"
ng-reflect-value="50757"
id="mat-option-0"
tabindex="0"
Please try with the below code: If possible please share the application URL then I can replicate it from my side.
WebElement option = driver.findElement(By.xpath("//mat-
option[contains(text(),'option text')]");
driver.waitUntilElementVisible(option, 10);
driver.findElement(option).click();
Also please refer link for more details on selecting a particular value in a dropdown without using the methods of Select class in Selenium
I am trying to perform drag and drop action on element but its not happening.
This is the snippet of the page I am working on. Here I am trying to drag and place the Tile "Time" in position of the Tile "Approvals".
Screenshot
This is the code I am using.
Code
String sSource = "//*[#id=\"PTNUI_LAND_REC14$1_row_0"]";
String sTarget = "//*[#id=\"PTNUI_LAND_REC14$1_row_1"]";
WebElement wSource = TestBase.wDriver.findElement(By.xpath(sSource));
WebElement wTarget = TestBase.wDriver.findElement(By.xpath(sTarget));
Actions aActions = new Actions(TestBase.wDriver);
Action aDragAndDrop = aActions.clickAndHold(wSource).moveToElement(wTarget).release(wTarget).build();
aDragAndDrop.perform();
HTML
Source Element
<div class="ps_grid-row nuitile rsz_w1 rsz_h1" id="PTNUI_LAND_REC14$1_row_0" tx="1.0577777777777777" ty="1" gx=".1.0577777777777777." gy=".1.">
<div class="ps_grid-cell">
<div id="win0divPTNUI_LAND_REC_GROUPLET$13" class="ps_box-group psc_layout nuilp " tabindex="0" draggable="true" aria-dropeffect="move" aria-grabbed="false" droppable="true">
<h2 class="ps_groupleth"><span class="ps-label" id="PTNUI_LAND_REC_GROUPLET_LBL$13">Approvals</span></h2>
Target Element
<div class="ps_grid-row nuitile rsz_w1 rsz_h1" id="PTNUI_LAND_REC14$1_row_1" tx="2.057777777777778" ty="1" gx=".2.057777777777778." gy=".1.">
<div class="ps_grid-cell">
<div id="win0divPTNUI_LAND_REC_GROUPLET$14" class="ps_box-group psc_layout nuilp " tabindex="0" draggable="true" aria-dropeffect="move" aria-grabbed="false" droppable="true">
<div id="win0groupletPTNUI_LAND_REC_GROUPLET$14" class="ps_box-grouplet"><img id="PT_PORTAL_CLEAR_DOT$14" class="ps_process" src="/cs/p91h25r2x/cache/PT_PORTAL_CLEAR_DOT_1.gif" alt=""></div>
<h2 class="ps_groupleth"><span class="ps-label" id="PTNUI_LAND_REC_GROUPLET_LBL$14">Time</span></h2></div></div>
Please let me know if you require any more details.
May be you have to target on draggable elements as given below.
String sSource = "//*[#id=\"win0divPTNUI_LAND_REC_GROUPLET$13"]";
String sTarget = "//*[#id=\"win0divPTNUI_LAND_REC_GROUPLET$14"]";
WebElement wSource = TestBase.wDriver.findElement(By.xpath(sSource));
WebElement wTarget = TestBase.wDriver.findElement(By.xpath(sTarget));
Actions aActions = new Actions(TestBase.wDriver);
aActions.dragAndDrop(wSource, wTarget).build().perform();
To Drag and Drop the Tile Approvals in position of the Tile Time you can use the following code block :
WebElement from = TestBase.wDriver.findElement(By.xpath("//div[#class='ps_box-group psc_layout nuilp ' and contains(id,'win0divPTNUI_LAND_REC_GROUPLET$13')]"));
WebElement to = TestBase.wDriver.findElement(By.xpath("//div[#class='ps_box-group psc_layout nuilp ' and contains(id,'win0divPTNUI_LAND_REC_GROUPLET$14')]"));
new Actions(TestBase.wDriver).dragAndDrop(from, to).build().perform();
System.out.println("Drag and Drop Completed");
My html sample code is,
<div class="list">
<div class="dropdown">
<ul role="menu">
<li class="rsbListItem">one</li>
<li class="rsbListItem">two</li>
<li class="rsbListItem">three</li>
<li class="rsbListItem">four</li>
<li class="rsbListItem">five</li>
<li class="rsbListItem">six</li>
<li class="rsbListItem">seven</li>
<li class="rsbListItem">eight</li>
</ul>
</div>
</div>
How can i write Selenium scripts for this, and each time when i run it should select randomly.
I have tried to pick random elements,but it's selecting the same element each time. Here is my code.
List<WebElement> options = driver.findElements(By.xpath("//*[#role='menu']"));
Random rand = new Random();
int list= rand.nextInt(options.size());
options.get(list).click();
Selenium is not my thing but
List<WebElement> options = driver.findElements(By.xpath("//*[#role='menu']"));
This will return a list of WebElement match your request. So you should get the element (should be only one I guess) to work with it. You could also use findElement I guess.
Then, you will need to get every Option in the select you have.
List<WebElement> selects = driver.findElements(By.xpath("//*[#role='menu']"));
Random rand = new Random();
for(WebElement select : selects){
List<WebElement> options = // get every option in it
int list = rand.nextInt(options.size());
options.get(list).click();
}
// Locate the dropdown menu
WebElement drpdown = driver.findElements(By.id("id of the dropdown menu"));
// click the dropdown menu
drpdown.click();
//Get the list of dropdown options
List<WebElement> itemsInDropdown = driver.findElements(By.id("id of the dropdown list"));
// Get the size of dropdown list
int size = itemsInDropdown.size();
// Generate the random number
int randomNumber = ThreadLocalRandom.current().nextInt(0, size);
// Clicking on random value
itemsInDropdown.get(randomNumber).click();
In a list of 8 Elements I would select the one that contains the search text in children div. I need this because the elements of the list changes order every time. Here I would like to select the one that contains the text "TITLE TO LISTEN". How do I scroll through the list and select the wish li?
Thanks in advance
Here one li:
...
<li id="3636863298979137009" class="clearfix" data-size="1" data-fixed="1" data-side="r">
<div class="userContentWrapper">
<div class="jki">
<span class="userContent">
TITLE TO LISTEN
</div>
<div class="TimelineUFI uiContainer">
<form id="u_0_b0" class="able_item collapsed_s autoexpand_mode" onsubmit="return window.Event && E" action="/ajax/ufi/modify.php" method="post" >
<input type="hidden" value="1" name="data_only_response" autocomplete="off">
<div class="TimelineFeedbackHeader">
<a class="ction_link" role="button" title="Journal" data-ft="{"tn":"J","type":25}" rel="dialog" href="/ajax/" tabindex="0" rip-style-bordercolor-backup="" style="" rip-style-borderstyle-backup="" >LISTEN</a>
</div>
</form>
</div>
</div>
</li>
</ol>
</div>
...
I tried this code, but it don't work because the elements ids change each time.
driver.findElement(By.xpath("//li[8]/div[2]/div/div[2]/form/div/div/span[2]/a")).click();
For example:
If text contain "TEXT TO LISTEN": li[3]/div[2]/div/div/div[2]/div/div/span
Link "listen" i want to click : li[3]/div[2]/div/div[2]/form/div/div/span[2]/a
here is number 3, but the order may change. I would first like to get that number and then click on the right link
Use this
driver.findElement(By.xpath("//li[contains(text(), 'Your text goes here')]"))
EDIT: just realised it's very old ques and you might have got ans by now, so for others who are looking for answer to this question.
You could get list of all li elements, and then search for specified text
for(int i=0; i< listOfLiElements.Count, i++){
if(listOfLiElements[i].FindElement(By.ClassName("userContent")).Text == "TITLE TO LISTEN")
{
correctElement = listOfLiElements[i].FindElement(By.TagName("a"));
i =listOfLiElements.Count;
}
}
Well, then just iterate through for each and ask if the current element has the right text inside it.
List<Element> listOfLiTags = driver.findElement(By.Id("yourUlId")).findElements(By.TagName("li"));
for(Element li : listOfLiTags) {
String text = li.getElement(By.ClassName("userContent").getText();
if(text.equals("TITLE TO LISTEN") {
//do whatever you want and don't forget break
break;
}
}
Note that this is much more easier with CssSelector API.
List<Element> listOfSpans = driver.findElements(
By.CssSelector("ul[id=yourId] li span[class=userContent]");
Now just iterate and ask for the right text:)
You can try this :
public void ClickLink()
{
WebElement ol =driver.findElement(By.id("ol"));
List<WebElement> lis=ol.findElements(By.tagName("li"));
ArrayList<String> listFromGUI=new ArrayList<>();
for(int i=0;i<lis.size();i++)
{
WebElement li=ol.findElement(By.xpath("//ol[#id='ol']/li["+(i+1)+"]/div[2]/div/div/div[2]/div/div/span"));
if(li.getText().trim().equals("TEXT TO LISTEN"))
{
WebElement link=ol.findElement(By.xpath("//ol[#id='ol']/li["+(i+1)+"]/div[2]/div/div[2]/form/div/div/span[2]/a"));
if(link.getText().trim().equals("LISTEN"))
{
link.click();
break;
}
}
}
}
I've got the following HTML code:
<div class="ui-selectmenu-menu" style="z-index: 1; top: 251px; left: 37px;">
<ul class="ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom" aria-hidden="true" role="listbox" aria-labelledby="gwt-uid-191-button" id="gwt-uid-191-menu" style="width: 270px; height: auto;" aria-disabled="false" aria-activedescendant="ui-selectmenu-item-999">
<li role="presentation" class="ui-selectmenu-item-selected">
All Applications</li>
<li role="presentation" class="">
Option Alpha</li>
<li role="presentation" class="ui-corner-bottom">
Option Beta</li>
</ul>
</div>
...
<div class="ui-selectmenu-menu"...>...</div>
I'm able to get the WebElement for ui-selectmenu-menu like this (there are many on the page; hence, the use of findElements) :
List<WebElement> dropdowns = driver.findElements(By.className("ui-selectmenu-menu"));
And the ul below it like this:
WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
I'm even able to grab all the li under the ddChild like this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li[*]"));
But the problem that I can't seem to figure out how to grab the text-value of the <a href="#nogo"... tag under each li element.
I'd like to be able to loop through all the ddOpts and grab the <a href="#nogo"... text values and save them to an ArrayList<String>.
So, for example, my first ArrayList<String> value would contain All Applications, then Option Alpha, then Option Beta, and then jump to the next ul element from the next dropdowns and do the whole process again, all while adding to the ArrayList<String>.
I'm sure its a simple solution but I've got limited experience with Selenium WebDriver.
Thanks!
PS: Is there a simple way to grab the child of a WebElement?
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
links.add(we.getText();
}
To extract the href attribute of the WebElement (referring to the anchor tag <a> in this example, do this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
// ADD all the href attribute strings to the list
links.add(we.getAttribute("href"));
}
This may also solve your problem:
List<WebElement> dropdowns = driver.findElements(By.className("x-combo-list"));
WebElement ddChild = dropdowns.get(0).findElement(By.className("x-combo-list-inner"));
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id=\"x-auto-98\"]/div[4]"));
for(WebElement we:ddOpts){
System.out.println(we.getText());
if(we.getText().contains("ROLE_MANAGER")){
we.sendKeys("ROLE_MANAGER");
we.click();
break;
}
}
the below code will select the OptionAlpha in the dropdown of the above HTML code
driver.findElement(By.xpath("//*[#class='ui-selectmenu-menu')).click();
driver.findElement(By.xpath("//*[#class='ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom']//**[text()='Option Alpha']")).click();
Please try the below code to get all the links in the <a href
List<WebElement> allLis = driver.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a");
// Looping through above list using for-each loop
for(WebElement eachLi : allLis) {
System.out.println(eachLi.getText());
}
Hope this helps.
href="#nogo" is same for all the anchor tags, so it might create ambiguity in selecting the item by the method
dropdowns.findelement(By.linktext("#nogo"));