驱动程序范围问题

问题描述 投票:0回答:1

我试图从电子商务网站选择产品“ADIDAS ORIGINAL”,但我的代码选择产品“ZARA COAT3”。不知道是什么原因?

我的代码是

package RahulShetty.SeleniumPractice;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;

 
public class AppTest {

@Test
public void practiceProject() throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    driver.get("https://rahulshettyacademy.com/client");
    
    String itema="ADIDAS ORIGINAL";

    driver.findElement(By.id("userEmail")).sendKeys("[email protected]");
    driver.findElement(By.id("userPassword")).sendKeys("Srinath@230");
    driver.findElement(By.id("login")).click();
    driver.manage().window().maximize();
    
    WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(5));
    
    
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class=\"card-body\"]")));
    //all products
    List<WebElement> items = driver.findElements(By.cssSelector("div[class=\"card-body\"]"));
    //Selecting adidas original webElement  
    WebElement element = items.stream()
            .filter(item -> item.findElement(By.tagName("b")).getText().equals(itema))
            .findFirst().orElse(null);
    //printing adidas original
    System.out.println("Selected item is :"+element.findElement(By.tagName("b")).getText());
    
    Thread.sleep(4000);
    //But In scope of adidas original element picking up ZARA COAT3
    element.findElement(By.xpath("//div[@class='card-body']/button[2]")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[aria-label=\"Product Added To Cart\"]")));
    
    driver.findElement(By.cssSelector("button[routerlink=\"/dashboard/cart\"]")).click();
    }
    }

我这里限制驱动程序范围来选择产品adidas Original。但我的代码是选择 ZARA COAT 3

 //all products
 List<WebElement> items = driver.findElements(By.cssSelector("div[class=\"card-body\"]"));
//only adidas original webElement
WebElement element = items.stream()
        .filter(item -> item.findElement(By.tagName("b")).getText().equals(itema))
        .findFirst().orElse(null);
//Here product is adidas original
System.out.println("Selected item is :"+element.findElement(By.tagName("b")).getText());

Thread.sleep(4000);
//But picking here ZARA COAT 3
element.findElement(By.xpath("//div[@class='card-body']/button[2]")).click();
selenium-webdriver selenium-chromedriver
1个回答
0
投票
element.findElement(By.xpath("//div[@class='card-body']/button[2]")).click();

上面的线似乎是问题所在。上述代码中的 XPath 定位并单击 “ZARA COAT 3”添加到购物车 按钮,而不是 “ADIDAS ORIGINAL”

解决方案: 请使用以下代码:

String itema="ADIDAS ORIGINAL";

element.findElement(By.xpath("//b[text()='" + itema + "']//following::button[2]")).click();

或者在 XPath 表达式中硬编码 ADIDAS ORIGINAL,如下所示:

element.findElement(By.xpath("//b[text()='ADIDAS ORIGINAL']//following::button[2]")).click();
© www.soinside.com 2019 - 2024. All rights reserved.