1818import org .openqa .selenium .ElementNotInteractableException ;
1919import org .openqa .selenium .WebDriver ;
2020import org .openqa .selenium .remote .RemoteWebElement ;
21+ import org .slf4j .Logger ;
2122import org .slf4j .LoggerFactory ;
2223
2324import software .xdev .selenium .elements .CanFindElements ;
3435@ SuppressWarnings ("java:S2160" )
3536public class ImprovedRemoteWebElement extends RemoteWebElement implements CanFindElements
3637{
37- protected String waitForServerLoadToFinishFunction ;
38+ protected Logger logger ;
39+ protected final String waitForServerLoadToFinishFunction ;
40+ protected boolean autoScrollIntoView = true ;
3841
3942 public ImprovedRemoteWebElement (final String waitForServerLoadToFinishFunction )
4043 {
4144 this .waitForServerLoadToFinishFunction = waitForServerLoadToFinishFunction ;
4245 }
4346
47+ public ImprovedRemoteWebElement withAutoScrollIntoView (final boolean autoScrollIntoView )
48+ {
49+ this .autoScrollIntoView = autoScrollIntoView ;
50+ return this ;
51+ }
52+
53+ protected Logger logger ()
54+ {
55+ if (this .logger == null )
56+ {
57+ this .logger = LoggerFactory .getLogger (this .getClass ());
58+ }
59+ return this .logger ;
60+ }
61+
4462 @ Override
4563 public WebDriver getWebDriver ()
4664 {
@@ -57,8 +75,7 @@ public void click()
5775 }
5876 catch (final ElementNotInteractableException ex )
5977 {
60- LoggerFactory .getLogger (this .getClass ())
61- .warn (
78+ this .logger ().warn (
6279 "Element can't be clicked via UI - executing JS click. "
6380 + "Please manually check if the element is accessible. "
6481 + "If the element is accessible consider calling performJsClick directly." , ex );
@@ -94,9 +111,16 @@ public void prepareForOperation()
94111
95112 public void scrollIntoViewIfRequired ()
96113 {
97- if (!this .isDisplayed ())
114+ try
115+ {
116+ if (this .autoScrollIntoView && !this .isDisplayed ())
117+ {
118+ this .executeScript ("arguments[0].scrollIntoView(true);" , this );
119+ }
120+ }
121+ catch (final ElementNotInteractableException ex )
98122 {
99- this .executeScript ( "arguments[0].scrollIntoView(true);" , this );
123+ this .logger (). warn ( "Element can't be scrolled into view" , ex );
100124 }
101125 }
102126
@@ -115,8 +139,7 @@ public void waitForServerLoadToFinish()
115139 final Boolean retVal = (Boolean )this .executeScript (this .waitForServerLoadToFinishFunction );
116140 if (retVal == null )
117141 {
118- LoggerFactory .getLogger (this .getClass ())
119- .warn ("waitForLoadToFinishFunction returned null! It should either return true or false" );
142+ this .logger ().warn ("waitForLoadToFinishFunction returned null! It should either return true or false" );
120143 }
121144 finished = Boolean .TRUE .equals (retVal );
122145 }
0 commit comments