Java 使用selenium WebDriver自动化Flipkart应用程序

Java 使用selenium WebDriver自动化Flipkart应用程序,java,selenium,selenium-webdriver,web-applications,testng,Java,Selenium,Selenium Webdriver,Web Applications,Testng,我的场景是搜索任何品牌的笔记本电脑,我需要在当前页面中选择最高价格,而不进行排序 @BeforeClass public void launchBrowser() { System.setProperty("webdriver.chrome.driver", "F:\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(

我的场景是搜索任何品牌的笔记本电脑,我需要在当前页面中选择最高价格,而不进行排序

@BeforeClass
public void launchBrowser()
{
    System.setProperty("webdriver.chrome.driver", "F:\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.flipkart.com");
}

  @Test(priority = 0)
  public void searchComputers() throws InterruptedException
  { 
    WebDriverWait wait = new WebDriverWait(driver, 40);
  wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@title='Search for Products, Brands and More']")));
  driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys("laptops");
  driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys(Keys.ENTER);
  wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']")));
  Thread.sleep(1000);
  JavascriptExecutor jse = (JavascriptExecutor)driver;
  jse.executeScript("window.scrollBy(0,250)", "");
  driver.findElement(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']")).click();
}

 @Test(priority = 1)
 public void searchHPComputers() throws InterruptedException
 {
   Thread.sleep(2000);
  WebDriverWait wait1 = new WebDriverWait(driver, 40);
  wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']")));
  Thread.sleep(1000);
  List<WebElement> element =  driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
  for(WebElement web : element)
  {
     String amount =  web.getText();
     int length = amount.length();
     String price = amount.substring(1, length);
     System.out.println("Amount : "+ price);
     Thread.sleep(1000);
  }

}


 @AfterClass
 public void closeBrowser() throws InterruptedException
 {
  Thread.sleep(2000);
  driver.close();
  driver.quit();
 }
我已经在eclipse控制台中打印了当前页面中的所有价目表。 但我不知道如何点击价格最高的笔记本电脑。
请帮助我解决此问题。

如果您想单击价格最高的搜索,您需要执行以下两项操作:

1评论如下行:

  JavascriptExecutor jse = (JavascriptExecutor)driver;
  jse.executeScript("window.scrollBy(0,250)", "");
2将searchHPComputers方法体替换为以下代码

      Thread.sleep(5000);
      driver.findElement(By.xpath("//li[text()='Price -- High to Low']")).click();

      Thread.sleep(5000);
      List<WebElement> element =  driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));

      element.get(0).click();

更新


我需要选择当前页面中价格最高的笔记本电脑,无需排序。
@Test(priority = 1)
public void searchHPComputers() throws InterruptedException
{

    Thread.sleep(2000);
    WebDriverWait wait1 = new WebDriverWait(driver, 40);
    wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']")));
    Thread.sleep(1000);
    List<WebElement> element =  driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
    List<WebElement> elementLink = driver.findElements(By.xpath("//div[@class='_3wU53n']"));

    int largestPrice =0, elementIndex =0, i=0;

    for(WebElement web : element)
    {
        String amount =  web.getText();
        int length = amount.length();
        String price = amount.substring(1, length);
        price = price.replaceAll(",", "");
        int priceInt = Integer.parseInt(price);
        System.out.println("Amount : "+ priceInt);
        Thread.sleep(1000);

        if(priceInt > largestPrice) {

            largestPrice = priceInt;

            elementIndex = i;
        }

        i++;
    }


    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].scrollIntoView(true);", elementLink.get(elementIndex-1));

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(elementLink.get(elementIndex)));

    elementLink.get(elementIndex).click();
}