英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论