【问题标题】:How to scroll UP to an element and click in selenium?如何向上滚动到一个元素并单击硒?
【发布时间】:2019-09-11 09:45:27
【问题描述】:

我有一个场景,系统需要向上滚动到页面左侧面板中的webelement,然后单击以执行其他操作。我尝试了以下方法,但都不起作用。请建议:

1.

 WebElement element = driver.findElement(By.xpath("element"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    Thread.sleep(500); 

2.

WebElement element = driver.findElement(By.xpath("element"));
 Actions actions = new Actions(driver);
        actions.moveToElement(ele);
        actions.perform();

【问题讨论】:

  • 你遇到了什么错误?
  • @frianH:我收到此错误“org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//* [text()='一些文字']"} "
  • 你可以尝试将你的定位器修改为 .//*[contains(text(),'some text')]
  • @Rahul 你确定你的定位器吗?似乎在页面中找不到元素。请分享html。
  • @frianH :在元素可见之前它工作正常,即当在该元素下方添加其他文本时,webdriver 无法向上滚动到所需的元素并执行操作。抱歉,html 不能共享,因为它是私有的。

标签: javascript java selenium selenium-webdriver webdriverwait


【解决方案1】:

UpDownscrollIntoView() 一个你必须为 visibilityOfElementLocated() 诱导 WebDriverWait 的元素,你可以使用以下解决方案:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2022-01-21
    • 2019-10-08
    相关资源
    最近更新 更多