在Selenium(Java)中按照变量双精度或浮点数滚动。

huangapple go评论73阅读模式
英文:

Scroll by a variable double or float in Selenium (Java)

问题

我是新手使用Selenium,在长时间的休息后重新学习编程,如果这个问题不够好,我表示抱歉。我有一个包含一堆瓦片的网页(通常每行4个,有很多行),其高度和宽度根据窗口的大小而变化。在每一行之后,我想要向滚动一个瓦片的高度。这是我的代码(其中eachtile是一个包含所有瓦片的列表):

double ImageHeight = eachtile.getSize().getHeight();
double f = 1.04 * ImageHeight;
((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -f);");

我尝试过在(0,-f)表达式中的引号和减号的各种变化,无论如何都会得到一个错误,说f未定义。有人知道该怎么办吗?

英文:

I'm new to Selenium and revisiting programming in general after a long hiatus, so apologies if this isn't a good question. I have a webpage with a bunch of tiles (typically 4 per row, a whole bunch of rows) whose height and width vary based on the size of the window. After every row I want to scroll up by the height of one tile. Here's what I have (where eachtile is a list containing all the tiles):

double ImageHeight = eachtile.getSize().getHeight();
double f = 1.04*ImageHeight;
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,-f);");

I've tried all variations of quotes and minus signs in the (0,-f) expression and no matter what I get an error saying that f is undefined. Does anyone know what to do here?

答案1

得分: 0

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,arguments[0]);",-f);

Thanks!!

英文:

Try this

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,arguments[0]);",-f);

Thanks!!

答案2

得分: 0

只返回翻译好的部分:

或者尝试另一种方式,每次滚动到列表中的下一个项目:

for (WebElement tile : tileList) {
    js.executeScript("arguments[0].scrollIntoView();", tile);
}
英文:

Or try another way, scroll to the next item in the list each time:

for (WebElement tile : tileList) {
    js.executeScript("arguments[0].scrollIntoView();", tile);
}

huangapple
  • 本文由 发表于 2020年8月7日 22:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63304255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定