javascript - Selenium ChromeDriver 如何点击 x,y 像素位置为负的元素? C#

标签 javascript c# css google-chrome selenium

我正在使用一个带有弹出式表格的网站。例如,如果用户将鼠标悬停或单击链接,则会出现一个带有数据表的小弹出窗口。同一页面上有许多此类链接。从表格中提取数据涉及通过按顺序单击链接依次打开和关闭每个弹出窗口。

关闭弹出窗口需要单击弹出窗口右上角的小“X”按钮。大约 99% 的时间,这不是问题。然而,大约 1% 的时间,弹出窗口的顶部将在屏幕上方,具有负的“y”像素位置,隐藏关闭弹出窗口的“X”按钮 - 反过来,给它一个负的' y' 像素位置。通过手动操作(手动移动鼠标),我可以调整 Chrome 窗口的大小,并且弹出窗口将“捕捉”回窗口,“y”像素位置至少为零。

我无法使用 Selenium 命令复制弹出窗口的手动重新居中。我能找到的最接近的命令是 MoveToElement,它有时有效,但有时无效(我不明白为什么我看到部分成功,但这是这个问题的题外话)。

据我所知,Selenium 不允许我与具有负 x 或 y 像素位置的元素进行交互。

这是我目前正在尝试的,但收效有限:

// example begins with pop-up already displayed. Data has been extracted.
// We are now ready to close the pop-up by clicking the 'X' button.

// here we move to the table, otherwise the detail popup close button will be off screen. Warning: this works with partial success. 

var actions = new Actions(Session.Driver);
actions.MoveToElement(table);
actions.Perform();

// Must close unless the popup may (or may not) cover the next link.
var closeButton = Session.Driver.FindElement(By.Id(id))
    .FindElements(By.CssSelector("a.cmg_close_button"))
    .FirstOrDefault();
if (closeButton != null)
{
    Wait.Until(d => closeButton.Displayed);

    if (closeButton.Location.Y < 0 || closeButton.Location.X < 0)
    {
        Log.Error("Could not close button. The popup displayed the close_button off-screen giving it a negative x or y pixel location.");
    }
    else
    {
        closeButton.Click();
    }
}

请提出处理弹出窗口负 x,y 像素位置的策略。

我正在使用 C# 4.6、Selenium 2.45、ChromeDriver (Chrome) 和 VS2015 CE IDE。

最佳答案

当链接靠近 View 的边框时,页面似乎没有正确设置弹出窗口的位置。

要克服这个问题,您可以先将目标元素滚动到 View 的中心,然后再移动到它上面:

// scroll the link close to the center of the view
Session.Driver.ExecuteScript(
  "arguments[0].scrollIntoView(true);" +
  "window.scrollBy(-200, -200);" ,
  table);

// move the mouse over
new Actions(Session.Driver)
  .MoveToElement(table)
  .Perform();

关于javascript - Selenium ChromeDriver 如何点击 x,y 像素位置为负的元素? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38463792/

相关文章:

css - 如何防止在我的 Angular 组件 View 中出现垂直滚动条?

javascript - 从 JavaScript 数组中删除负数

javascript - 如何清除缓存中下载的图像

javascript - 未捕获的语法错误 : Unexpected string in my javascript file

c# - 如何使用 .ToDictionary 忽略我的文本文件中的重复项,我总是收到错误消息 : "The item with the same key has already been added"

javascript - 用 JavaScript 隐藏一个 div

javascript - 在发送请求之前更改图像的 src

c# - 一种存储信息的方法

c# - UDP 客户端 - 创建套接字时出现异常通常只允许每个套接字地址(协议(protocol)/网络地址/端口)使用一次

javascript - 无法点击 html tabcontent 中的其他选项卡