System.setProperty("webdriver.chrome.driver", "C:\\Users\\Testing\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://jpetstore.cfapps.io/login");
driver.manage().window().maximize();
driver.findElement(By.name("username")).sendKeys("Testing");
driver.findElement(By.name("password")).sendKeys("test@123");
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath("//div[@id='SidebarContent']/a[contains(@href,'FISH')]/img")).click();
在这里,我能够创建动态 xpath driver.findElement(By.xpath("//div[@id='Catalog']//parent::td//preceding-sibling::td//a//following:: td//a")).click(); driver.findElement(By.xpath("//div[@id='Catalog']//parent::a//following::a[contains(text(),'Add to Cart')]")).点击(); driver.findElement(By.xpath("//div[@id='BackLink']//a[contains(text(),'返回主菜单')]")).click(); driver.findElement(By.xpath("//div[@id='SidebarContent']//a[@href='/catalog/categories/FISH']")).click();