Selenium: - 元素无法滚动到视图中

问题描述 投票:7回答:6

嗨,我正面临一个错误,如下所述。我无法点击屏幕截图中提到的买家按钮。我也试过等待,睡觉功能。但无法超越这一点。任何人都可以帮助我。

任何人都可以帮助我: - Inspect Element code is Code是: -

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>
selenium selenium-webdriver webdriver
6个回答
8
投票

首先,验证元素是否在您的框架中。

如果不是,则需要切换到正确的框架才能单击元素:

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

此外,您还可以执行许多步骤,以便在单击不同UI元素时提高稳定性:

  • 明确地等待它在DOM中的存在
  • 滚动到元素视图中
  • 检查是否可点击

它有助于稳定吗?

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")));

所以,例如,如果我正在研究site,我将使用Wait.until(ExpectedConditions.presenceOfElement(By.class("article-feed-title")));


4
投票

可能是由现有的bug引起的:https://bugzilla.mozilla.org/show_bug.cgi?id=1422272


2
投票

您可以尝试使用此版本滚动到元素的x,y位置:

public static void scrollIntoView(WebElement ele) {
    ((JavascriptExecutor)driver).executeScript("window.scrollTo(" + ele.getLocation().x + "," + ele.getLocation().y + ")");
}

2
投票

我在Firefox中有类似的问题但是(第一次当我点击悬停菜单时它能够启用主元素然后我点击子元素但第二次当我试图点击另一个子元素时我收到错误)。错误:“元素无法滚动到视图中”。我在另一个适合我的窗口中打开了下一个子菜单。

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");
}

1
投票

而不是scrollinToView只需单击该元素

driver.execute_script("arguments[0].click();", element)

0
投票

我在Firefox中遇到了同样的问题(在Chrome和Opera中运行良好)。尝试将JavascriptExecutor更改为Actions:

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
© www.soinside.com 2019 - 2024. All rights reserved.