@@ -192,32 +192,37 @@ public function goto_widget_selection() {
192192
193193 $ this ->driver ->find_element_and_click ( '#login-button ' );
194194
195- $ window_handles = $ this ->driver ->get_driver ()->getWindowHandles ();
196- $ this ->driver ->get_driver ()->switchTo ()->window ( end ( $ window_handles ) );
195+ $ window_handles = $ this ->driver ->get_driver ()->getWindowHandles ();
196+ $ new_window_handle = end ( $ window_handles );
197+
198+ $ this ->driver ->get_driver ()->switchTo ()->window ( $ new_window_handle );
197199
198200 // driver currently on tawk.to OAuth login popout.
199201
200- // handle currently logged in page.
201- $ allow_id = '#allow ' ;
202- $ allow_button = $ this ->driver ->find_and_check_element ( $ allow_id );
203- if ( false === is_null ( $ allow_button ) ) {
204- $ allow_button ->click ();
205- $ this ->driver ->get_driver ()->switchTo ()->window ( reset ( $ window_handles ) );
206- $ this ->driver ->wait_for_frame_and_switch ( '#tawk-iframe ' , 10 );
207- return ;
208- }
202+ // currently logged in page not shown because driver creates new browser instance.
209203
210204 // handle login page.
211205 $ this ->driver ->find_element_and_input ( '#email ' , $ this ->tawk ->username );
212206 $ this ->driver ->find_element_and_input ( '#password ' , $ this ->tawk ->password );
213207 $ this ->driver ->find_element_and_click ( 'button[type="submit"] ' );
214208
215- // handle consent page.
216- $ allow_id = '#allow ' ;
217- $ allow_button = $ this ->driver ->find_and_check_element ( $ allow_id );
218-
219- if ( false === is_null ( $ allow_button ) ) {
220- $ allow_button ->click ();
209+ $ start = microtime ( true );
210+ while ( ( microtime ( true ) - $ start ) < 5 ) {
211+ // consent already allowed, window will automatically close.
212+ $ current_handles = $ this ->driver ->get_driver ()->getWindowHandles ();
213+ if ( ! in_array ( $ new_window_handle , $ current_handles , true ) ) {
214+ break ;
215+ }
216+
217+ // handle consent page if shown.
218+ try {
219+ $ this ->driver ->find_element_and_click ( '#allow ' );
220+ break ;
221+ } catch ( \Exception $ e ) { // phpcs:ignore
222+ // consent button not found, do nothing.
223+ }
224+
225+ usleep ( 10000 );
221226 }
222227
223228 // go back to tawk-iframe frame.
0 commit comments