Java Selenium Web 驱动程序:无法滚动到视图中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22588096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:41:28  来源:igfitidea点击:

Selenium web driver: cannot be scrolled into view

javaseleniumselenium-webdrivertestngselenium-ide

提问by Naif

I am using Selenium IDE and Selenium web driver testng in eclipse .. my testing is against ZK application ..

我在 Eclipse 中使用 Selenium IDE 和 Selenium Web 驱动程序 testng .. 我的测试是针对 ZK 应用程序的 ..

the test case works fine on Selenium IDE ..

测试用例在 Selenium IDE 上运行良好..

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://*****/>
<title>work it2</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">work it2</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/xxx</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>//li[2]/div/div/div/span</td>
    <td></td>
</tr>
<tr>
    <td>pause</td>
    <td>3000</td>
    <td>3000</td>
</tr>
<tr>
    <td>doubleClick</td>
    <td>//div[2]/div[2]</td>
    <td></td>
</tr>
<tr>
    <td>pause</td>
    <td>3000</td>
    <td>3000</td>
</tr>
</tbody></table>
</body>
</html>

but when I run it in eclipse with selenium web driver (testng) I got an error ..

但是当我使用 selenium web 驱动程序(testng)在 eclipse 中运行它时,我得到了一个错误..

    selenium.open("xxx");
selenium.click("//li[2]/div/div/div/span");
Thread.sleep(3000);
selenium.doubleClick("//div[2]/div[2]");
Thread.sleep(3000);

I also changed the code to

我也将代码更改为

 driver.get("xxx");

        driver.findElement(By.xpath("//li[2]/div/div/div/span")).click();
        Thread.sleep(3000);
        WebElement ee = driver.findElement(By.xpath("//div[2]/div[2]"));
        Actions action = new Actions(driver);
        action.doubleClick(ee).perform();
        Thread.sleep(3000);

also getting the same error ...

也得到同样的错误...

the error was in this line

错误在这一行

//div[2]/div[2]

com.thoughtworks.selenium.SeleniumException: Offset within element cannot be scrolled into view: (87, 118): [object XrayWrapper [object HTMLDivElement]] Command duration or timeout: 63 milliseconds Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15' System info: host: 'EnD', ip: '192.168.17.76', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51' Session ID: 3b79783c-2558-4c87-bd51-a72821696040 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=27.0.1}]

com.thoughtworks.selenium.SeleniumException:元素内的偏移量无法滚动到视图中:(87, 118):[object XrayWrapper [object HTMLDivElement]] 命令持续时间或超时:63 毫秒构建信息:版本:'2.39.0',修订版: 'ff23eac', 时间: '2013-12-16 16:11:15' 系统信息: 主机: 'EnD', ip: '192.168.17.76', os.name: 'Windows 7', os.arch: ' amd64',os.version:'6.1',java.version:'1.7.0_51' 会话 ID:3b79783c-2558-4c87-bd51-a72821696040 驱动信息:org.openqa.selenium.firefox.FirefoxplatformDriver Capabilities [ ,acceptSslCerts=true,javascriptEnabled=true,cssSelectorsEnabled=true,databaseEnabled=true,browserName=firefox,handlesAlerts=true,browserConnectionEnabled=true,webStorageEnabled=true,nativeEvents=false,rotatable=false,locationContextEnabled=true,applicationCacheEnabled=true,takesScreenshot=true,version=27.0.1}]

回答by TDHM

I'm not sure but try and see if following works for you. First, you've to make that element visible before interacting with it -

我不确定,但请尝试看看以下是否适合您。首先,您必须在与其交互之前使该元素可见 -

WebElement element = driver.findElement(By.xpath("//div[2]/div[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

The above code will scroll down till the element is visible and then you can click on it.

上面的代码将向下滚动直到元素可见,然后您可以单击它。

回答by TDHM

Naif,

奈夫,

Actually, your above question is different than the actual question hence you should have asked it as a separate question. Still, I'm answering to your previous question.

实际上,您的上述问题与实际问题不同,因此您应该将其作为单独的问题提出。不过,我正在回答你之前的问题。

The error is because the element you're trying to click on isn't visible. Before you click on element, it should be visible. You can do this by following -

错误是因为您尝试单击的元素不可见。在单击元素之前,它应该是可见的。您可以通过以下方式做到这一点 -

WebElement element = driver.findElement(By.xpath("//div[2]/div[2]"));
WebDriverWait wait = new WebDriverWait(driver, 20); //here, wait time is 20 seconds

wait.until(ExpectedConditions.visibilityOf(element)); //this will wait for elememt to be visible for 20 seconds
element.click(); //now it clicks on element

If above doesn't work, you can click on element by executing javascript(but this isn't a good practice)

如果以上不起作用,您可以通过执行 javascript 来单击元素(但这不是一个好习惯)

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

回答by lnarasimhan

I was getting this error in slightly different context where I was trying to click anchor tag with the selenium RemoteWebDriver (I was trying to replace WebDriver). The fix was identifying the right set of capability for the driver for eg:

我在稍微不同的上下文中遇到此错误,我尝试使用 selenium RemoteWebDriver 单击锚标记(我试图替换 WebDriver)。修复是为驱动程序确定正确的功能集,例如:

capability = DesiredCapabilities.chrome(); capability.setPlatform(Platform.WIN10); capability.setCapability("version", "66");

capability = DesiredCapabilities.chrome(); capability.setPlatform(Platform.WIN10); capability.setCapability("version", "66");

回答by Zoran Pandovski

Try to execute the script and click the element

尝试执行脚本并单击元素

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