如何滚动到特定元素并单击它?

时间:2018-01-25 09:51:57

标签: java selenium selenium-webdriver

如何滚动到特定元素并单击它?

以下是我尝试过的代码:

Random R = new java.util.Random();
List<WebElement> Top_Stores =driver.findElements(By.xpath(".//*[@id='go_body']/main/section/div/div[2]/div/div/div[2]/aside/div[1]/section[1]/a"));
WebElement randomElement = Top_Stores.get(R.nextInt(Top_Stores.size()));
String S=randomElement.getText();
System.out.println(S);
Actions actions = new Actions(driver);
actions.moveToElement(randomElement);
actions.perform();
randomElement.click();

1 个答案:

答案 0 :(得分:0)

您可以使用javascript滚动:

public void scrollToElement(WebElement element) {
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}