Selenium: The element could not be scrolled into view
Asked Answered
A

9

17

Hi I am facing an error which is mentioned below. I am unable to click on the button of Buyer as mentioned in the screenshot. I have tried wait, sleep functions too. But unable to move beyond this. Can anyone help me in it.

Can anyone please help me in this: Inspect Element code is .

Code is:

driver.findElement(By.name("login")).click();  //Click on login button
    System.out.println("hello world-----4");

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }   
    System.out.println("hello world-----5");
    WebElement element = driver.findElement(By.xpath("//*
[@id=\"modeuser\"]/div/ul/li[3]"));
    ((JavascriptExecutor) 
driver).executeScript("arguments[0].scrollIntoView(true);", element);
    element.click();                                                                                    
//Click on usertype



Error:

Exception in thread "main" 
org.openqa.selenium.ElementNotInteractableException: Element <li 
class="buyer_border changeusermode "> could not be scrolled into view
Build info: version: '3.9.0', revision: '698b3178f0', time: '2018-02-
05T14:56:13.134Z'
System info: host: 'CLAVAX-PC-93', ip: '192.168.2.122', os.name: 'Windows 
10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, 
browserVersion: 58.0.2, javascriptEnabled: true, moz:accessibilityChecks: 
false, moz:headless: false, moz:processID: 14260, moz:profile: 
C:\Users\Rahul\AppData\Loca..., moz:webdriverClick: true, pageLoadStrategy: 
normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: 
false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}


HTML is :

<div class="right hide-on-med-and-down head_right_mar" id="modeuser">
               <!--  <div class="toggleWrapper">
                  <input class="dn" type="checkbox" id="dn" value="1"/>
                  <label class="toggle" for="dn"><span class="toggle__handler"></span></label>
                </div> -->

                <div class="right_toggle">
                                            <ul>
                        <li data-get="seller" class="changeusermode active">
                            <span>Seller</span>
                                                                <span class="nav_span">On</span>

                        </li>


                        <li class="mid_toggle">  
                            <div class="switch">
                            <label>

                              <input class="changeusermode_btn" type="checkbox" data-on="Yes" data-off="No">
                              <span class="lever"></span>
                            </label>
                          </div>
                        </li>


                        <li data-get="buyer" class="buyer_border changeusermode ">
                            <span>Buyer</span>

                                <span class="nav_span">Off</span>                                     

                        </li>
                    </ul>
                </div>


            </div>
Appreciative answered 1/3, 2018 at 8:9 Comment(3)
Please post the html. Is it sitting in an Iframe? Have you tried focusing on it? Can you click it's parent element? possible duplicate of elementnotinteractableexceptionSindee
Posted.. You may check nowAppreciative
Possible duplicate of How to resolve ElementNotInteractableException in Selenium webdriver?Noxious
C
19

First of all, verify that the element is in your frame.

If it's not, you will need to switch to the correct frame in order to click on the element:

driver.switchTo().frame(driver.findElement(By.name("iframeWithElement")));

In addition, there is a number of steps you can do in order to improve the stability while clicking on different UI elements:

  • Explicitly wait for it's presence in the DOM
  • Scroll into the element view
  • Check if clickable

Does it help the stability?

WebDriverWait wait = new WebDriverWait(driver, 3);
JavascriptExecutor js = ((JavascriptExecutor) driver);

//presence in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

//scrolling
WebElement element = driver.findElement(By.id("ID")));  
js.executeScript("arguments[0].scrollIntoView(true);", element);

//clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));

So for example, if I'm working on the site, I will use:

Wait.until(ExpectedConditions.presenceOfElement(By.class("article-feed-title")));
Cristalcristate answered 4/3, 2018 at 12:21 Comment(0)
P
4

You could try this version which scrolls to the x,y position of the element:

public static void scrollIntoView(WebElement ele) {
    ((JavascriptExecutor)driver).executeScript(String.format("window.scrollTo(%s,%s)", ele.getLocation().x, ele.getLocation().y);
}
Permanent answered 1/3, 2018 at 16:13 Comment(2)
This worked for me. However I must stress that readability is very important in test code. String.format("window.scrollTo(%s,%s)", ele.getLocation().x, ele.getLocation().y) is a better way to represent the JS script.Hulbert
Thanks for the suggestion and upvote, your edit was gladly accepted.Permanent
E
4

Could be caused by this existing bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1422272

Elbaelbart answered 19/3, 2018 at 19:31 Comment(0)
R
4

Instead of scrollingToView just click that element

driver.execute_script("arguments[0].click();", element)
Revenue answered 18/7, 2018 at 23:44 Comment(0)
G
2

I had same problem in Firefox (in Chrome and Opera it works well). Try change JavascriptExecutor to Actions:

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Guild answered 10/8, 2018 at 14:52 Comment(0)
B
2

I had similar problem in Firefox but (First time when i am clicking on Hover-menu it is able to enable the main element and then i am clicking on the sub-element but for the second time when i tried to click on another sub element i am getting error). Error: "element cannot be scrolled into view". I opened the next sub menu in another window that worked for me.

WebElement element = driver.findElement(By.xpath("//div[2]/ul/li[3]/a"));
WebDriverWait wait = new WebDriverWait(driver, 30); //here, wait time is 20 sec
wait.until(ExpectedConditions.visibilityOf(element)); //this will wait for element to be visible for 20 seconds
element.click(); //now it clicks on element
Thread.sleep(8000);
driver.findElement(By.xpath("//li[3]/ul/li/a")).click();

if(driver.getTitle().equals("Invoice Search")) {

test4.log(LogStatus.PASS, "Navigated to Invoice Search Page");
}
Bank answered 12/11, 2018 at 6:32 Comment(0)
L
0

I know this question expects a java answer, but I hope this comment will help to understand one possible source of this error, through this python explanation.

So, I have encountered this issue as well. I managed to solve based one these comments https://mcmap.net/q/206234/-selenium-python-error-element-could-not-be-scrolled-into-view

https://mcmap.net/q/694527/-selenium-the-element-could-not-be-scrolled-into-view

My addition to this problem is that, you may want to check your CSS if this issue comes up. In my case it turned out that the element I tried to click was at (x: -9999, y: 538) in one of the .css files. No wonder it could not be scrolled in the view.

Also this CSS mistake will be most likely throw an exception if you use the following to do clicking for example.

from selenium.webdriver.common.action_chains import ActionChains

# ...

tc = self.browser.find_element(*self.TC_CHECKBOX)
ActionChains(self.browser).move_to_element(tc).click(tc).perform()
Lemur answered 19/3, 2021 at 0:1 Comment(0)
G
0

If someone still has the issue, I solved it by setting a window size when in headless mode. In Java, this is done like driver.manage().window().setSize(new Dimension(5120, 2880));

Gallium answered 29/8, 2022 at 20:29 Comment(0)
S
-1

I had the same problem (but in NodeJS), not Java.

I used the Firefox driver on MacOS.

The problem was: Firefox was slow on MacOS. So it was still starting and ignored the first test commands. When it was intended to click a button, that button was actually not there.

What fixed my problem?

Before testing anything, I wrote this sleep-function:

const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs))

and I executed it with:

await driver.get('http://localhost:8080/webapp/')
await sleep(5000)
// tests after that

alternative:

sleep(5000).then(() => {
  // tests here
});

That was enough time for Firefox to start and to take the test commands properly then.

Strew answered 2/10, 2019 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.