Facing an issue while selecting drop down in Selenium webdriver - java

I am trying to select a drop down using selenium web driver, using this code:
WebElement admissionSource = driver.findElement(By.name("ABC"));
Select admissionSource_select= new Select(admissionSource);
Thread.sleep(10000);
Here, ABC is the value name attribute for that element.
It is selecting as expected, however once it moves to the next drop down just below that, it deselects the previous one.
Things I've tried:
1) After filling the next drop down, going back and filling the previous drop down again. However this second attempt selects the first drop down but deselects the next drop down(as is the application). Filling the next drop down again throws below exception:
org.openqa.selenium.StaleElementReferenceException: Element is no longer valid
2) Thread.sleep()
3) Implicit wait
4) Explicit wait
Please suggest on how to resolve this issue.

StaleElementException occurs when your referenced element is no more available or the element/page is refreshed. In your case, selecting from first dropdown refreshes/reloads your second dropdown. Hence, the StaleElementException occurs. To overcome this, after selecting from first dropdown, get the location of the second dropdown and select your element afresh each time.

This issue was coming because the in the 2nd dropdown, the options were dependent on the first one. So earlier i was just waiting for the dropdown box to get loaded. However in this scenario, even though the dropdown box had loaded but the dropdown options had not yet loaded for the 2nd drop down. Hence it was giving StaleElement Exception.
The way to tackle this is to use FluentDriver wait on the any (random) dropdown option and once it is loaded then only proceed further.

Between these two sentences from your question:
Select admissionSource_select= new Select(admissionSource);
Thread.sleep(10000);
Won't you need to do something like:
admissionSource_select.selectByVisibleText("Abc");
Where Abc is the name visible for that element.
You have just declared the drop-down element to be an instance of Select class. Is merely that sufficient? May be actually selecting the element was missing when you tried. You can try with adding above statement and then waiting while 2nd drop-down populates.

Related

Can we use one pageobject among different pages?

I am working on one selenium project. My scenario is, I have multiple forms one after another. After filling them I have to click on the Next button. I have 4 pages with the next button and they have the same XPath. When I try to use the same web element for the next button it works once but when I try to use that same thing again on a different page It shows element intreactable error.
I would try the following to identify why you get the interactable error (it would help to give us the error at least as is found on the Selenium Documentation)
try to click using javascript executor,
try to add webelements for each action you need to perform (so 4 webelements for each 'Next' button). If it works in this way maybe the webelement is not refreshed after you use it (maybe is a static field). Try also to work with Page Factory pattern.
If nothing works, we should receive more information to be able to help.

Waiting for element to be really clickable in Selenium [duplicate]

This question already has answers here:
Element MyElement is not clickable at point (x, y)... Other element would receive the click
(5 answers)
Closed 3 years ago.
I do have a Select and I try to wait patiently for it to be available, but that won't do.
WebDriverWait wait = new WebDriverWait(getWebDriver(), 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("myxpath")));
Select select = new Select(element);
select.selectByVisibleText(text);
When I run the code on this particular Element I get an error message:
... is not clickable at point (1311,183) because another element <p class="ng-tns-c4-0"> obscures it
I suppose this is because the page has these annoying status messages showing on the upper right corner for some seconds and then fading away. Though they are far away from my dropdown, they still seem to obscure it.
The whole thing works if I add a 2 second explicit wait, but that somehow offends my sense of stile and I most likely would end up spreading them all over the tests and slow them down a lot.
Is there any generic way of waiting for an element not to be obscured? I mean a way without having to know which particular message pops in.
POSTSCRIPT:
Since I cannot add an answer on my own, I add this postscript. In the end I have settled for this solution:
protected void secureSelect(String text, Select select) {
try {
select.selectByVisibleText(text);
} catch(ElementClickInterceptedException e) {
Wait.seconds(2);
select.selectByVisibleText(text);
}
}
I know that these problems will occur all over the application with different messages of the same type. So in case of an error I just try once again and the let it fail if it goes wrong again.
To answer your last question, is there a generic way of waiting for an element to not be obscured? Outside of elementToBeClickable, not really. Web pages are dynamic and each one of them loads content differently so there's not exactly a catch-all for this. It is unfortunate that elementToBeClickable is encountering a ClickIntercepted error for your scenario, but there are a few workarounds.
You could try adding an additional wait to wait on invisibilityOfAllElements for the status messages that keep popping up, if these are what is getting in the way:
wait.until(ExpectedConditions.invisibilityOfAllElements(By.xpath("//p[contains(#class, 'ng-tns')]")));
This may encounter a timeout exception depending on the nature of displayed p elements compared with the rest of the page content. Another possible workaround would be leaving your code as it, and using Javascript to select elements from the Select instead:
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("myxpath")));
JavascriptExecutor js = (JavascriptExecutor)driver;
// expand the Select dropdown
js.executeScript("arguments[0].click();", element);
// wait for Select options menu to expand
WebElement optionToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xPathForDropdownOption")));
// select desired option
js.executeScript("arguments[0].click();", optionToClick);
The above code is a bit more 'explicit' in what we are waiting on -- specifically, the dropdown option value. Clicking both the Select dropdown and the dropdown option with Javascript are meant to be workarounds for the ClickIntercepted error.

How to perform some actions on each value from a drop down using selenium java?

How to perform some actions on each value from a drop down using selenium java?
I am using below code. Please help.
WebElement bldgs=Fn_GetWebElement(CreateSSIObject.getProperty("Bldgselect"));
Select Bldg_select=new Select(bldgs);
List<WebElement> dropdownvalues = Bldg_select.getOptions();
int count=dropdownvalues.size();
System.out.println("Toatl number of values are :"+count);
for(int i=1;i<26;i++)
{
if(dropdownvalues.get(i).isEnabled())
{
dropdownvalues.get(i).click();
System.out.println("Not Working :"+i);
}
}
In order to select an option from select drop down, we need to call the methods on the select instance instead of the webelements.
For Eg,
Select Bldg_select=new Select(bldgs);
Bldg_select.selectByIndex(4); // selects 4th element in the drop down
you can't use click on the available options in the drop down, and this needs to be handled by using the select instance that we had created.
Refer this link to get an idea of various available options for selection a value in the drop down.Selenium Select Drop Down Options.
Thanks.
If you want to just print the values present in drop-down use following code, there's no need to click each item & select as .getOptions() already does that for you (i.e. fetches all the values inside Select Tag) so you can just traverse through the list and getText() of each element in the list.
for(int i=1;i<26;i++)
{
System.out.println("Not Working :"+ dropdownvalues.get(i).getText());
}
You're missing a click to expand the dropdown before trying to select the option. Meaning it will be trying to click on an element that's not visible. Add in:
bldgs.click();
Before your if to expand the dropdown

How to get new appearing links after clicking one in Selenium webdriver?

I'm writing a test when you get all the links from the website and click them. But I need to click some links that in the beginning are hidden or some links that appear only in other pages. Till now, I'm only got to the point that test gets all the active links from homepage and clicks them. I'm new to Selenium webdriver and java, so can you suggest how should I write the test that checks for new appearing links after clicking one or something similar?
I usually write my Selenium tests much more specifically, but were I to attempt this I suspect I would start by making use of findElements(By.tagName("a")) to get all currently available anchors, probably put them into a data object which included if that anchor as been clicked yet and put those data objects into a Set. Map that Set to the currentUrl to keep track of what links were found on what pages. After a click (and recording that you clicked that anchor in its data object) you could check the currentUrl (without any #s) with the last one (without any #s) to determine if that click loaded a new page. If the urls match, I would call findElements again and add those to the existing Set. If they don't match repeat the process for the new currentUrl. Some additional things to be aware of would be handling new Windows and frames, which would require a switchTo and iterating through all the frames (and nested frames).

Element in View port issue [Peculiar]

I am getting the following error in a peculiar fashion.
The point at which the driver is attempting to click on the element was not scrolled into the viewport.
The user story goes like this, if a selection is made in a check group, corresponding div with few fields will be enabled. Similarly for n-number of selection there will be n-number of DIVs. When i select only one option in the div, it works as expected but not for the multiple options.
i have read some stuffs and tried all the possible combination about the above said view port issue. My work around attempts and the outcomes are given below.
1) Tried with the Advanced interaction method. (Action class) - The same exception
2) Accessed with java-script executor- same exception
few more will be updated soon.
I have tried all the combination of locating mechanism. Everything works perfect and the element is in visible mode. But when i perform any operation on the second DIV values i am getting the above said exception.
what can be possibly done to get the element in working mode?
EDIT :
1.The DIV elements all are in view port and they all are accessible individually.
2. Div are enabled as per the check box selected
3.In a scenario, if we choose two check box, after a first selection you can edit the DIV and for the second selection the DIV will not be accessible.
4. both DIVs are identical with respect to the elements/attributes/properties of the elements.
5. When a Active div is edited no other div will be available in the UI.
For some reasons i was not able to produce the Reproducible code here.
i sorted out the problem. There was actually a frame for every DIV. these frames are just hidden after the edit and a fresh frame is opened every time, so when i access the second div i had to choose the latest frame.
i just polled for the number of frame and proceed with the latest one.

Categories