首页 > 解决方案 > Selenium Java,测试名称和价格时某些产品的 Np 价格

问题描述

我需要在搜索中获取所有产品及其价格,必须识别没有价格的产品,有人可以帮助我如何识别没有价格的产品并打印出有价格的产品吗?

public static void main(String[] args) throws InterruptedException {

   System.setProperty("webdriver.chrome.driver","C:\\Users\\casa\\Desktop\\Drivers\\chromedriver.exe"); 
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--lang=en");
    ChromeDriver driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.get("https://www.amazon.com");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    String expetedResultSrh = "1 a 48 de más de 100,000 resultados para \"phone case\"";
    
    Thread.sleep(4000);
    driver.findElement(By.id("twotabsearchtextbox")).sendKeys("phone case"+ Keys.ENTER);
    WebElement valsch = driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/span/h1/div/div[1]/div/div"));
    
    if (valsch.getText().contains("phone case")) {
    System.out.println("1.  "+"Result from search ::::"+ valsch.getText()+ "......PASSED");
    }else {
        System.out.println("Expected result failed.....UI shows Result: "+ valsch.getText()+".....FAILED");
    }
    //leftPanecat = Left Pane Category for Material
    WebElement leftPanecat =  driver.findElement(By.xpath("//*[@id=\"p_n_feature_ten_browse-bin-title\"]/span"));


    System.out.println("\n"+"2.  "+leftPanecat.getText());
    System.out.println("\n"+"3.  Clicking... See More Options"+"....PASSED");
    driver.findElement(By.xpath("//*[@id=\"filters\"]/ul[4]/li[11]/span/div/a/span")).click();
    Thread.sleep(2000);
    //check box Carbon Fiber
    if(!driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011 > span > a > div > label > i")).isSelected()) {
        
        driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011 > span > a > div > label > i")).click();
    }else {
        System.out.println("This Box has been seleted");
    }
    //print label
    System.out.println("\n"+"4.  "+"Sucessfully Selected Material   :  "+driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011")).getText());
    
    ****List<WebElement> productsName = driver.findElements(By.tagName("h2"));
    List<WebElement> prodPrice = driver.findElements(By.xpath("//span[@class='a-price-whole']"));
    List<WebElement> prodPriceFrac = driver.findElements(By.xpath("//span[@class='a-price-fraction']"));
    try {
    
    for(int i=0;i<productsName.size();i++) {
        
        //System.out.println("\n"+i+".  "+productsName.get(i).getText());
        
        
        
  
            
        //System.out.println(prodPrice.get(J).getText());

if(productsName.get(i).getText().equals("需要帮助?")) {

            productsName.remove(i);
}
System.out.println(i+". "+productsName.get(i).getText()+"------Price:: "+prodPrice.get(i).getText()+"."+prodPriceFrac.get(i).getText());
    }
            
}**
              
          
        
            
        
    catch(IndexOutOfBoundsException e) {
        
    System.out.println("Passed ");
    }**

标签: java

解决方案


I have directly used your product page Url below after the execution of 
your script.

driver.get("https://www.amazon.com/s? 
k=phone+case&rh=n%3A2407760011%2Cp_n_feature_ten_browse- 
bin%3A17731927011&dc&qid=1624167494&rnid=11043306011&ref=   
sr_nr_p_n_feature_t en_browse-bin_5");

    List<WebElement> element = driver.findElements(By.xpath(
    "//* 
  [@id=\"search\"]/div[1]/div/div[1]/div/span[3]/div[2]/div/div/span       
  /div/div/div[4]/div[2]/a/span"));

    for (WebElement l : element) {
        if(l.getAttribute("innerText")==null) {
            System.out.println("not available");
        }else {
             
  System.out.println("available"+l.getAttribute("innerText"));
        }
    }
I have tried to get the innerText for the product which contains 
the price, It ignores the product for whom the price is not 
available, Can you check with it.

推荐阅读