How to continue webdriver/selenium after error - selenium

I'm doing a test bot with webdriver. I have a scenario where it clicks on a button, a new windows open, and it searches for an element by a specific xpath, but sometimes there is no such element because it can be disabled and i get this error: org.openqa.selenium.NoSuchElementException. How can i bypass it/continue the bot so it just closes the new windows if it doesn't find the element with that xpath and just continue with the code?

In Java :
List<WebElement> foundElement = driver.findElements(By.xpath("<x-path of your element>"));
if (foundElement.size() > 0)
{
// do whatever you want to do in **presence** of element
} else {
// do whatever you want to do in **absence** of element
}

You need to surround the click event with a try/catch statement, and inside the catch statement check if the exception is the one you are trying to bypass:
try {
driver.findElement(by).click();
} catch(Exception e) {
if ( !(e instanceof NoSuchElementException) ) {
throw e;
}
}

Related

Switch to top-level frame with Selenium

What is the best way to switch to the top-level frame in Selenium if I'm unsure of the nesting level?
I tried using this code, but ParentFrame() doesn't throw an exception if you are already at top-level so it doesn't work.
while (true)
{
try { driver.SwitchTo().ParentFrame(); } catch { break; }
}
If you want to return to the main content from Iframe use this
driver.switchTo().defaultContent();

protractor + cucumber - element not visible even though element is visible

this.When(/^the user clicks on login button$/, function () {
return browser.wait(wagLoginPage.loginPage.signIn.isPresent().then(function (visible) {
if(visible){
console.log("element is visible !!!!!!!");
wagLoginPage.loginPage.signIn.click().then(function(){
expect(visible).to.be.true;
});
}
else{
expect(visible).to.be.true;
}
}, function () { chai.assert.isFalse(true, "SingIn is not visible!") }));
});
My test randomly fails in the above step. For the above code, in console window protractor prints 'element is visible'. But if I perform click event on the element it throws element is not visible exception.
Update
Questions is answered here
Your element is present, but it's probably not visible.
Try this:
return browser.wait(wagLoginPage.loginPage.signIn.isDisplayed().then(function (visible){
//Your stuff
}
Note, I'm using isDisplayed() vs. isPresent().
isPresent() is useful if you are checking if an element is on the page, but may or may not be visible.
isDisplayed() is useful if you are checking if an element is visible on the page.

Geb: Getting value from dropdown datalist (Element not visible error)

I need to select an option from a datalist which gets populated after a short time duration. This is what I have tried-unsuccessfully- so far.
Module module.groovy
inputBox { $("input", 0) }
clientListItemOne{ $("datalist option", 0) }
clientListItemTwo { $("datalist option", 3) }
clientListItemThree { $("datalist option", 7) }
UI Test
try{
module.inputBox.click().click()
waitFor("quick") { module.clientListItemOne.displayed }
module.clientListItemOne.click()
}
The result is that even though the dropdown datalist is visible, the browser gets stuck in the waitFor loop and quits.
Without using the waitFor loop, I get an 'element not visible' error.
How do I remedy this situation?
Thanks a lot in advance.

Close editor using IEditorreference? ( eclipse RCP )

I have a UI in which when I select an item (in a tree) and then press a button "add", I get a new editor. With each item I can get an editor. (but all have the same ID)
My purpose is to close only the editor of item1, for example, when I press "save".
I'm able to close all the editors with:
getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);
But not only the one that I need to close.
I think, this problem might be solved using the IEditorreferences but don't know exactly how to do it! :(
please help :)
List<IEditorReference> editors = new ArrayList<IEditorReference>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editor : page.getEditorReferences()) {
editors.add(editor);
}
}
}
getSite().getWorkbenchWindow().getActivePage().closeEditor(editors.get(index)????,true);
Editor can be tracked with the editor-input. The object representing item1 must be part of your editor-input...
Something like:
// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);
// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getEditorReferences();
List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().equals(tmpInput)) {
relevantEditors.add(iEditorReference);
}
}
PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.closeEditors(
(IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
.size()]), true);
Make sure that you have overriden the equals and hashCode of your EditorInput to check the equality of the wrapped MyObject-instance
When opening your editor you have to track the mapping between your items and the associated opened IEditorReference This can be done for example using a simple HashMap object.
thanks to Tom, your answer helps a lot.
As each IEditorInput could have its name that can be set, we also can use like followings:
// String str =.....
// str, could be an editor's property
if (iEditorReference.getEditorInput().getName().equals(str))
Besides, it shall throw PartInitException like this:
//....................
try {
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().getName().equals(str)) {
relevantEditors.add(iEditorReference);
}
}
} catch (PartInitException e) {
e.printStackTrace();
}
//...................

How to create an if statement in selenium that would run if a certain box was not checked?

I am trying to run an if statement in selenium that would verify if a checkbox was checked and if it was not perform a certain action. I tried
if (selenium.verifyChecked("checkbox")=false){
//perform actions
} else {
//perform different actions
};
and it said that that did not work. How would you do it?
The Selenium command isChecked returns a boolean, so you should be able to do the following:
if (selenium.isChecked("checked")) {
//perform actions
} else {
//perform different actions
};
if (selenium.verifyChecked("checkbox")=false){
Is wrong. It's assigning false to the return value of the function, which is clearly wrong.
It should be:
if (selenium.verifyChecked("checkbox") == false) {