Selenium web driver: cannot be scrolled into view

2019-01-15 08:39发布

问题:

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

the test case works fine on 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.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}]

回答1:

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)

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


回答2:

Try to execute the script and click the element

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


回答3:

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.



回答4:

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:

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