【问题标题】:The Xpath Webelement appears only when I make Scroll downXpath Webelement 仅在我向下滚动时出现
【发布时间】:2019-08-17 14:15:20
【问题描述】:

我正在尝试通过向下滚动在 xpath 中获取一个 Web 元素,但这很困难,因为该 Web 元素仅在我向下滚动时出现,我的意思是该 Web 元素仅在我向下滚动时出现,所以我不知道该怎么做,因为也许我必须先向下滚动,然后再向下滚动,直到出现我想要的 web 元素,这取决于我想要获得的元素。 我尝试用它向下滚动,但没有从下面得到 web 元素。

  JavascriptExecutor js = (JavascriptExecutor) getDriver();
        js.executeScript("window.scrollBy(0," + bajar + ")");
        waitFor(3).seconds();

我也试过这个,但没有找到 WebElement。

  Point loc = getDriver().findElement(findBy(String.format
            ("//div[@class='windowViewMode-maximized active lafPageHost']//tbody//td//span/a[@title='%s']//ancestor::tr//child::td//span/a[@title='%s']",
                    nombre_cuenta, accion_relacional))).getLocation();
    System.out.println(loc);
    JavascriptExecutor js = (JavascriptExecutor) getDriver();
    js.executeScript("javascript:window.scrollBy(0," + loc.y + ")");
    System.out.println("El valor de y es " +loc.y);
    System.out.println("Se hizo el scroll");
    waitFor(1).seconds();
    WebElement registroLista = findBy(String.format
            ("//div[@class='windowViewMode-maximized active lafPageHost']//tbody//td//span/a[@title='%s']//ancestor::tr//child::th//span/a[@title='%s']",
                    nombre_cuenta,accion_relacional));
    baseMetodosPagina.clickJavaScript(registroLista);

顺便说一句,我正在使用 selenium 和 java。

【问题讨论】:

  • 您可以在函数中使用 WebDriverWait 和滚动。捕获超时异常并再次运行滚动/等待。仅当您确定元素最终会在那里时才这样做......或者至少放入一个在这么多超时后退出的计数器。
  • 这是我一直想到的,但我不知道怎么做。

标签: selenium selenium-webdriver xpath


【解决方案1】:

尚未测试,但类似的方法可能有效:

int LicksToTheCenterOfATootsiePop = 100;
int NumTries = 0;
int ec_Timeout = 10;

public void ScrollUntilElementFound(string xpath)
{
 try
  {
   wait = new WebDriverWait([your webdriver], ec_Timeout);
   List<WebElement> elements =  wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xpath)));   
    if (elements.size() > 0) //may not need this, just return since timeout should be thrown if it's zero
       {
       return;
       }


   }
   catch (Exception ex)
   {
   NumTries++;
     if (NumTries<LicksToTheCenterOfATootsiePop)
        {
        ((JavascriptExecutor)[your webdriver]).executeScript("window.scroll(0, document.height);");
        ScrollUntilElementFound(xpath);
        }
     else 
        {
        return;
        }
    }
 }

ScrollUntilElementFound("[your XPATH to find Element]");
// now find element, and set NumTries back to zero if you plan on re-using

【讨论】:

    【解决方案2】:

    您可以尝试使用此方法在网页上查找特定的网页元素。

    JavascriptExecutor js = (JavascriptExecutor) getDriver();
    js.executeScript("arguments[0].scrollIntoView(true);", web element locator);
    //use explicit wait for synchronization purposes.
    

    通过这种方式,您的页面将滚动到该 Web 元素,然后对该元素执行您需要的操作。

    【讨论】:

      【解决方案3】:

      您需要滚动到该特定元素并编写显式等待条件,直到该元素可见,然后执行操作。下面是示例代码:

      import org.openqa.selenium.By;
      import org.openqa.selenium.JavascriptExecutor;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.openqa.selenium.support.ui.ExpectedConditions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class ScrollToElement{
      
      public static void main(String[] args) {
      
          System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
          WebDriver driver = new ChromeDriver();
          WebDriverWait w= new WebDriverWait(driver, 10);
          driver.get("https://semantic-ui.com/elements/button.html");
          driver.manage().window().maximize();
      
        // Scroll to specific Element
      
           JavascriptExecutor js=(JavascriptExecutor) driver;
           WebElement e = 
           driver.findElement(By.xpath("//button[contains(text(),'Negative')]"));
           js.executeScript("arguments[0].scrollIntoView();",e);
           w.until(ExpectedConditions.visibilityOf(e));
      
      
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        相关资源
        最近更新 更多