Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an active window title between two browser tabs/windows using Selenium Java

Tags:

java

selenium

I can switch between two tabs/windows but my requirement is to know or get active window between them.

In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.

I tried to use JNA Api to get the active window and its title but my web page is
remotely located.

Perfect solution is greatly appreciated.

Thanks

like image 828
Rahul Chintakunta Avatar asked Apr 25 '26 22:04

Rahul Chintakunta


2 Answers

driver.getTitle() will give you the title of the page that you can use to determine which page you are on or if you are on the page where you want to be and then use the logic to switch window if required. getTitle() returns a String and you can use one of the string methods to compare the title, for example:

String title = getDriver().getTitle();

if(!title.equals("Expected Title")) {
        //may be you would like to switch window here
    } 
like image 80
AGill Avatar answered Apr 27 '26 13:04

AGill


String title = driver.getTitle()

This will give you the title of the page which you can refer to using Selenium to figure out which page the driver is currently on.

like image 23
Chris Hawkes Avatar answered Apr 27 '26 11:04

Chris Hawkes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!