How to jump to specific command if element is not found

This is my first use, and I have some questions.

I recorded my activities like below.

  1. Open the website
  2. Click the Login button
  3. Open Member profile and accept friend requests.

But if I have already Login, Log in button disappears, and the Recorder stops, shows element not found

Is there any way to do this:

  1. Try to Click the login button.
  2. If the button isn’t there, jump the task to Open Member profile.

I appreciate your help!

Use if.

StoreElementPresent | id=foo | login_button
if | login_button==true |
click | id=foo |
else | |
rest of the script
endif | |

3 Likes

Solved. THANK YOU!

Can I ask one more question?
I want to press Ctrl+T to open a new tab on chrome :slight_smile:

Hi, you can use keys chord:

WebUi.sendKeys(findTestObject(‘object’), Keys.chord(Keys.CONTROL, ‘T’))
And don’t forget to add import org.openqa.selenium.Keys as Keys

Don’t know if it will work, but you can also do it using js f.e. js.executeScript(‘window.open();’)
Than just WebUI.switchToWindowIndex(window + 1)
(where ‘window’ is int window = WebUI.getWindowIndex() whitch gives you an index of your current tab opened)
Regards

[info] Executing: | sendKeys | CONTROL | T |

[info] Wait until the element is found

[error] Implicit Wait timed out after 10000ms

[error] Element CONTROL not found

I can’t add that directly to the source code because I’m using Recorder.
I have three inputs, Command and Target and Value.

What can I do?

Different question should be in separate topic on forum.
SandKeys can couse a problem sometimes - i read on the forum about that.
I see 2 solutions to that:

  1. Use runScript with JS code openning new tab.
  2. Click an element/link on page witch open something in new tab and then work on this new tab. You can add open command then so it will open adress in thid new tab.

So how would that JS script go exactly ?

I personally didn’t use it but check this: https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript

It’s go like this:

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

runScript | window.open(“https://www.google.com”,“_blank”) |

the above works just fine

How do we send key combinations though ?

1 Like

There should be a workaround in JS as well i think.

selenium.runScript(“window.open(“https://www.google.com”,"_blank")”)

:sweat_smile:
I don’t know why it doesn’t work.

For me it works but it’s open new window and then new tab not just new tab in current window. I have to let open pop-ups in this new window. If you run this code and it’s open new window llok at address bar of ypur browser there should be a meesage that it want to open pop-up, just click on it and let it always open popup.

With runScript it opens two new google.com tabs, but when i use addScript i opens just one new tab.