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();
}
Related
Using the select method I can't select the dropdown
I tried using normal and findelement method and also index selection method
Here am using input value in different file
click(driver,"id",prop.getProperty("state"));
click(driver,"xpath",prop.getProperty("voption"));
and
Index selection method
and
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
Expected result :
need to click the dropdown
Actual result :
"stale element reference: element is not attached to the page document"
it showing error message like this
Try locating the WebElement first, then select by visible text.
WebElement dropDown = driver.findElement(By.id("state"));
new Select(dropDown).selectByVisibleText("ANTARCTICA");
If this does not work, but you dont get the StaleElementReferenceException change the selection option to selectByIndex() or selectByValue().
If you get StaleElementReferenceException pointing to line with driver.findElement(...) it means that something on your page has changed so some kind of wait mechanism should be introduced. In such case I suggest to locate the dropDown using FluentWait.
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.
I have two links in pageA. when I click the 1st link it redirects to another page called pageB and do some certain jobs and returns back to pageA. From here it should again click to the 2nd link but instead it says page has reloaded and no cache available.
//List of all tickets
for(WebElement ticket: ticketList){
List<WebElement> ticketCells = ticket.findElements(By.tagName('td'));
if(ticketCells.get(4).getText().equalIgnoreCase("Some Text")){
ticketCells.get(2).click(); //Redirects to pageB
.....
do some job
.......
//Finally clicking on the 'SAVE & BACK' button which should return to previous
//page and pick the 2nd ticket from the list of all tickets (1st for loop)
driver.findElement(By.id("save&back")).click();
}
}
Here though it it is going back to previous page pageA but unable to pick the 2nd element from for loop for next operation.
Any thoughts on how to make it work.
I think that what you want is the equivalent to press the Back button of your browser, right?
If its this, try that:
webDriver.navigate().back();
If you want to navigate to back or previous page in java selenium browser. You can try
webDriver.navigate().back();
If you want to navigate to back or previous page in python selenium browser. You can try
driver.back();
If you want to navigate to back or previous page in JavaScript selenium browser. You can try
driver.execute_script("window.history.go(-1)");
#try-catch-finally explained it very clearly. The below code is exactly what you need to handle the error.
for(int i = 0; i <2 ;i++){
ticketList = driver.findElements(selector);
ticket = ticketList.get(i);
List<WebElement> ticketCells = ticket.findElements(By.tagName('td'));
if(ticketCells.get(4).getText().equalIgnoreCase("Some Text")){
ticketCells.get(2).click(); //Redirects to pageB
.....
do some job
.......
//Finally clicking on the 'SAVE & BACK' button which should return to previous
//page and pick the 2nd ticket from the list of all tickets (1st for loop)
driver.findElement(By.id("save&back")).click();
}
}
The below test is written using Selenium-
I have a test case where I click on a "next>>" button and the application performs some calculation on server and renders to a new page, but after clicking the "next>>" button sometimes it takes 1-2 minutes to open the next page.
In this test case I am verifying the title of next page to confirm that the next page actually opened and after that I perform further actions to this next page.
But the problem I am facing is when I click on "next>>" button and server take some time, the code to verify the title gives stale Element Reference Exception. If I remove the verify Title code block it perform further actions without any error.
Please suggest some solution to this problem.
I'm not sure this will help as you didn't share the whole code
but you can make selenium wait until the page is loaded before assertion
Try below code
WebDriverWait wait = new WebDriverWait(driver, 180);
wait.until(ExpectedConditions.visibilityOf(// element in the page to be
displayed after you press next);
By using this before the assertion Selenium will not make the assertion until the page is loaded
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.