Unable to select all the elements at one time - java

Please look into the attached screenshot
I need to select all the data in 1st box at one time.Currently it allows me to select First name and then click on Arrow and then i need to click on Middle name and click on Arrow to move to box 2.I need to do individually for all the data in Box1 to move to Box2.Also i tried drag and drop Its not working Even manually also its not allowing to drag/drop from Box1 to Box2
Can we select all the elements in the box 1 at a time and then click on arrow to move to box2?Please help me out with this issue..
(Note : Below is the Html code for the Box 1)
`

As per provided comment xpath
/html/body/div[4]/div/div[2]/div[2]/div/form/table/‌​tbody/tr[1]/td[1]/select/option
will retrieve list of all options nothing but list of webelements. so use findElements here and collect that list
List<WebElement> elements = driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/‌​tbody/tr[1]/td[1]/select/option"));
//use loop here
System.out.println(elements .size());
for(int i=0;i<=elements .size();i++) {
//use click on option
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/‌​tbody/tr[1]/td[1]/select/option")).click(); //it will click first option by default
//write command to click on arrow
//so it will loop upto list size nothing number of options and always select first and clicks on >>
}
thank you,
Murali

You can achieve this solution very easy by Jquery.
the following code very useful to you.
$().ready(function()
{
$('#right_arrow_id').click(function()
{
return
!$('#firstboxid_here option:selected').clone(true).appendTo('#second_box_id_here');
});
for removing options from second box.
$('#left_arrow_id_here').click(function()
{
$('#second_box_id_here option:selected').remove();
});
});

By looking at the first xpath "/html/body/div[4]/div/div[2]/div[2]/div/form/table/‌​tbody/tr[1]/td[1]/select/option[i]", I understood that it is implemented like a dropdown. Hence, you can use "Select" class in selenium. Please find the below code for the same.
// Initializing the Select class
Select names = new Select(driver);
// Retrieving all the options in the dropdown
List<WebElement> allNamesList = names.getOptions();
// Looping through the list
for(WebElement eachName : allNamesList) {
// Clicking on each element in the first box
eachName.click();
// Your Code to click on Arrow button
}
Hope this helps.

Related

Selenium Java- Drop down selection where Style- display is none

When I try to select a drop down with Style display: none;
option 1:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");
The above code renders error:
element not interactable: Element is not currently visible and may not
be manipulated
Option 2:
WebElement hiddenWebElement =driver.findElement(By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
Option2 recognizes the drop down but unable to select an item from the drop down.
Any help would be appreciated.
Tried few options i see in the site but didtn't help much
You could try altering the style attribute using Javascript, as such:
hiddenWebElement = driver.findElement(
By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript(
"arguments[0].style.display = 'block';", hiddenWebElement);
After that, you can try a Javascript click or regular click.
Could you right click -> Inspect and see if it is really a dropdown? If drop down is visible and still getting this error, it might not an html dropdown. It may look like one but can only confirm if you inspect or look at code.
If element is an html drop down, use the following code to see if it works:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Coordinates coordinate = ((Locatable) sysDropDown).getCoordinates();
coordinate.onPage();
coordinate.inViewPort();
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");

Selenium Webdriver with Java - Click on an hyperlinked Dropdown Container

I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.

Reload page, but keep DOM saved

Could someone please help me in following case.
Have a page on which exiss click button with 7 options in it.
Found those seven options and save it in a list
WebElement List
Wanted to use for loop in order to pickup every element and check if every button is cliked.
Now comes tricky part:
That dropdwon is not Select one type and is shown after click on button named Report
So, in summary I have first to click to open dropdown (on Report button),
then choose one option
and after that message on page appears such is:
report sent succesfully or error
But, button for activating dropdown disapper (after click on sending report) and will not appear again until page has been reloaded.
In order to choose next option I must reload page, but then I loose saved options and iteration is uselless.
Tried reloading page with
driver.navigate().refresh();
As a result, I have one pass in loop and after that error:
stale element exception element still exists no DOM attached, the reference is lost
So, my question: Is there any other way in Java & Selenium to keep DOM and reload same page in a same time?
Thank you in advance
You just need to refetch the collection of options inside the loop and access it by index.
By buttonLocator = By.id(""); // the button that when clicked exposes the dropdown
By optionsLocator = By.id(""); // the options in the dropdown
driver.findElement(buttonLocator).click();
List<WebElement> options = driver.findElements(optionsLocator);
for (int i = 0; i < options.size(); i++)
{
driver.findElements(By.id("")).get(i).click(); // click the option
// code that detects success or error
driver.navigate().refresh();
}

Drop down button is not responding with selenium in java

I want to click on drop down button and select one among one and click on it.I tried by many ways but it is not working. Attached image of html code, Anyone help me regarding this.
Below is my code which i tried
driver.findElement(By.id("homepages_dropdown")).click();
/*List<WebElement> allElements = driver.findElements(By.xpath("//*[#id=\"homepages_item_AccountManagerDashboard\"]"));
for (WebElement element: allElements)
{
System.out.println(element.getText());
}*/
Just use "Select" to create an object that handles dropdowns etc. You can use "Select" to select element by it's text, index and a bunch of other options. It's the easiest way to handle such elements as dropdown and comboboxes.
new Select(driver.findElement(By.id("homepages_dropdown"))).selectByVisibleText("Your option's text");
This isn't the sharpest way to do it, but if you can't get developers to make it a specific select element via HTML it may have to do. I have stuff like this in my solution where there are custom dropdowns.
You can start by creating a dropdown element and then click a single record in it if that's what you're trying to do, in your case it looks like it is the span within the highlighted button element, so you can do something like the following:
driver.findElement(By.id("homepages_dropdown")).click();
driver.findElement(By.xpath("//li//a[#id='homepages_item_AccountManagerDashboard']").click();
The above statements will click the dropdown then click the element with the id of homepages_item_AccountManagerDashboard.
You can also create a function that clicks any li of based off of a parameter you pass into a method you create, consider this if you may select many options in your dropdown.
public void SelectItem(string itemText)
{
var dropdownElement = driver.findElement(By.id("homepages_dropdown"));
var selectionItem = driver.findElement(By.xpath("//li[text()='" + itemText + "']");
dropdownElement.Click();
selectionItem.Click();
}
This is only really a valid option if you are working through the Page Object Model, but it's the best way to make things work consistently long term.
To click on Dropdown button and Select the Option with text as Analytics you can use the following code block :
driver.findElement(By.xpath("//div[#id='homepages']/button/span[#id='homepages_dropdown']")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='homepages']/ul/li/a"));
for (WebElement element : allElements)
if(element.getAttribute("innerHTML").contains("Analytics"))
{
element.click();
break;
}
System.out.println("Option with text as Analytics is selected");
I found solution myself,Below code worked
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("homepages_dropdown")));
// drop down
driver.findElement(By.id("homepages_dropdown")).click();
Thread.sleep(2000);
// selecting configuration
driver.findElement(By.id("homepages_item_ConfigurationHomepage")).click();
Thread.sleep(2000);

Click event on sort option doesn't work: Selenium Webdriver

There is a sort option on the website which upon clicking presents options which i want to click on.
Using the click() event for that sort label, does nothing.
That highlighted part is the label which is clickable in image, though click event for the same doesn't work via script.
From that list, i want to click on Most listings option.
Code:
WebElement op= driver.findElement(By.id("sortBy-label"));
op.click();
I am not sure why it is not working without looking into the application. I can come up with something with the shared HTML. Make sure you use explicit waits and wait for the search results to be displayed before clicking on the SORT OPTIONS. Why don't you try this?
//wait for the search results. Use explicit waits and try this
driver.findElement(By.linkText("SORT BY: MOST ACTIVE")).click();
List<WebElement> lis=driver.findElement(By.id("sortBy-menu")).findElements(By.tagName("li"));
for (WebElement li : lis) {
if (li.getText().trim().equals("Most listings")) {
li.click();
}
}

Categories

Resources