gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:15 24 4
gpt4 key购买 nike

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

关闭弹出窗口需要单击弹出窗口右上角的小“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com