簡體   English   中英

如何將Selenium WebDriver中的頁面滾動到特定鏈接

[英]How to scroll the page in selenium webdriver to specific link

我無法滾動到指定的值。 在下面的鏈接

http://www.flipkart.com/mobiles/samsung~brand/pr?sid=tyy,4io&otracker=hp_nmenu_sub_electronics_0_Samsung

我需要單擊排除缺貨,因為我需要滾動網頁並單擊它。

通過自動化IAM無法實現這一目標

iam使用以下代碼獲取坐標

    Point hoverItem =driver.findElement(By.xpath("//li[@title='Exclude Out of Stock']/a")).getLocation();
    System.out.println("dsds"+hoverItem);
    ((JavascriptExecutor)driver).executeScript("return window.title;");    
    Thread.sleep(6000);
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");"); 

並使用以下代碼向下滾動iam。

JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollBy(143,1459)", "");

然后我點擊使用鏈接的鏈接。

driver.findElement(By.xpath("//li[@title='Exclude Out of Stock']/a")).click();

上面的代碼不起作用。

如果我運行代碼並手動滾動,則單擊指定的值,

如何克服這種情況。

   package Examples;

import java.util.concurrent.TimeUnit;

//import org.junit.BeforeClass;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Flipkart {
    public static WebDriver driver;


    @BeforeClass
    public void beforeClass()
    {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);


    }

    @Test

    public void FlipkartTest() throws InterruptedException
    {

        driver.get("https://www.flipkart.com/");
        driver.manage().window().maximize();


        navback();
        System.out.println("22  "+driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).getText());
        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).click();


        driver.findElement(By.xpath("//li[@title='Exclude Out of Stock']/a")).click();

    }

    public void navback()
    {
          WebElement we = driver.findElement(By.xpath("//html/body/div/div/div[2]/div/div/ul/li/a/span"));
          Actions action = new Actions(driver);
          action.moveToElement(we).build().perform();
    }



    @AfterClass
    public void tear()
    {
       // driver.quit();
   }
}

Selenium Webdriver自動滾動以定位元素,因此只要用於定位元素的選擇器正確,只有findElement()click()可以工作。

該元素需要花費一些時間才能顯示,因此我將使用隱式或顯式wait等待它

 new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#availability input.facetoption"))).click();
  1. 嘗試使用By.lintext而不是xpath-By.linkText(“排除缺貨”)
  2. 正如Amith提到的那樣,單擊會自動滾動。
  3. 在嘗試js之前,請嘗試使用Actions方法。 Movetoelement也應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM