2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以使用我編寫的以下方法滾動(dòng)到特定元素。您只需要傳遞 Driver 對(duì)象。
// Method to scroll down to specific element
public static void scrollToElement(WebDriver driver) throws InterruptedException {
String key = "";
WebElement element = null;
JavascriptExecutor js = (JavascriptExecutor) driver;
element = driver.findElement(By.xpath(locator));
// This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(2000);
}

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
你能試試這個(gè)嗎,我希望window你的代碼中缺少全局變量
// scroll vertically
js.executeScript("window.scrollTo(0, 1000);")
// scroll horizontally
js.executeScript("window.scrollTo(1000, 0);")
//scroll to particular element
WebElement Element = driver.findElement(By.id("someID"));
js.executeScript("arguments[0].scrollIntoView();", Element);
添加回答
舉報(bào)