【问题标题】:Selenium web driver: cannot be scrolled into viewSelenium Web 驱动程序:无法滚动到视图中
【发布时间】:2014-04-30 13:14:57
【问题描述】:

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

测试用例在 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>

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

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

我也把代码改成了

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

同样的错误...

错误在这一行

//div[2]/div[2]

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.FirefoxDriver 功能 [{平台=XP,acceptSslCerts=true,javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handleAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true,nativeEvents=false,可旋转=false, locationContextEnabled=true,applicationCacheEnabled=true, 需要屏幕截图=true,版本=27.0.1}]

【问题讨论】:

  • eclipse selenium.doubleClick("//div[2]/div[2]")的这一行出现错误;
  • IDE 示例中的 doubleClick xpath 与 WebDriver 代码不同。这是故意的吗?
  • 抱歉打错了!
  • 我真的被困在这里了,有什么帮助吗?
  • 可能会出现此类错误,因为您尝试单击的元素不可见。然而,这是一个猜测。如果我们的计算机上没有出现同样的问题,很难提供帮助。您可以在 jsfiddle.net 上创建一个具有相同问题的示例页面吗?通常,制作一个简单的测试用例可以帮助您自己找出问题所在。

标签: java selenium selenium-webdriver testng selenium-ide


【解决方案1】:

奈夫,

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

该错误是因为您尝试单击的元素不可见。在单击元素之前,它应该是可见的。您可以通过以下方式执行此操作 -

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

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

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

【讨论】:

  • 谢谢,但它也不起作用..我确信该元素在浏览器上是可见的..它在 selenium IDE 上工作正常..但不知道为什么在 web 驱动程序中不起作用!
  • org.openqa.selenium.TimeoutException: 等待 20 秒后超时 [[FirefoxDriver: firefox on XP (d6ae4ad6-057c-460b-a730-a52f05fb536f)] -> xpath: // div[2]/div[2]]
  • 请尝试使用Javascript部分,它一定会工作。
  • WebElement element = driver.findElement(By.xpath("//div[2]/div[2]")); JavascriptExecutor js = (JavascriptExecutor)驱动程序; js.executeScript("arguments[0].click();", element);
  • 来自未来的你好。向下滚动到视图对我不起作用,即使该元素已经在视图中。起作用的是.click 通过executeScript 函数。在 python 中,API 调用是 `js.execute_script("argument[0].click();", element)。感谢您的解决方案!
【解决方案2】:

尝试执行脚本并点击元素

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

【讨论】:

  • 谢谢。 executeScript() 在这里与 javascript API 和 Firefox 68 一起工作。我猜默认 click() 函数的错误仅在单击 ./table/tr/td 内的元素时发生。
【解决方案3】:

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

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

上面的代码会向下滚动直到元素可见,然后你可以点击它。

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      我刚刚调整了浏览器的尺寸,效果就这样

      driver = webdriver.Firefox()
      driver.get(SOME_URL)
      driver.set_window_position(0, 0)
      driver.set_window_size(1024, 768)
      
      

      【讨论】:

      • 注意:我在 python 中做了这个,但我想在 Java 中同样的方法可能会起作用。
      猜你喜欢
      • 1970-01-01
      • 2015-09-17
      • 2017-09-03
      • 1970-01-01
      • 2017-07-09
      • 2014-04-06
      • 2023-04-04
      • 2018-08-09
      • 2016-08-11
      相关资源
      最近更新 更多