Selenium Java无法访问弹窗元素问题及代码求助
问题:无法定位购物车弹窗内的元素
点击「加入购物车」按钮后会弹出带「前往购物车」按钮的弹窗,但无法访问弹窗内任何元素,尝试多种定位器和元素列表均无效。
我的代码
driver.findElement(By.cssSelector("//a[normalize-space()='Single Arrow Slim Fit Sweatshirt in Cotton Blend']")).click(); // List<WebElement> elements = driver.findElements(By.className(".CartItem.MiniCartPopup.is-separate")); //List<WebElement> elements = driver.findElements(By.xpath("//div[@id='MiniCart-items']/div")); // System.out.println(elements.size()); //System.out.println(driver.findElements(By.xpath("//*[@id='MiniCart-items']/div")).size()); String kk = driver.findElement(By.cssSelector("a[aria-label='icon-bag'] span")).getText(); System.out.println("the items on products are"+ kk); String comp=driver.findElement(By.linkText("Single Arrow Slim Fit Sweatshirt in Cotton Blend")).getText(); System.out.println("the selected items have text" +comp);
弹窗HTML代码(仅鼠标悬浮时可查看)
<div class="CartItem-content"><div class="CartItem-thumbAndBadge"> <a class="CartItem-pdpLink" href="/shop-off-white-single-arrow-slim-fit-sweatshirt-in-cotton-blend-for-men-215022969_22.html"> <img src="//ounass-prod1.atgcdn.ae/small_light(p=thmb,ch=158,cc=fafafa,of=webp)/pub/media/catalog/product/2/1/215022969_orange_in.jpg?1654148030.1771" alt="Single Arrow Slim Fit Sweatshirt in Cotton Blend"> </a> </div> <div class="CartItem-info"><h3 class="CartItem-brand"><a href="/shop-off-white-single-arrow-slim-fit-sweatshirt-in-cotton-blend-for-men-215022969_22.html">Off-White</a> </h3> <h4 class="CartItem-name"><a href="/shop-off-white-single-arrow-slim-fit-sweatshirt-in-cotton-blend-for-men-215022969_22.html">Single Arrow Slim Fit Sweatshirt in Cotton Blend</a></h4><div class="CartItem-attribute"> <span class="CartItem-attributeName">Colour</span> <span class="CartItem-attributeValue">Orange</span></div> <div class="CartItem-attribute"><span class="CartItem-attributeName">Size</span> <span class="CartItem-attributeValue">S</span> </div> <div class="CartItem-attribute"><span class="CartItem-attributeName">Qty</span> <span class="CartItem-attributeValue">1</span></div> <div class="CartItem-badge"></div> <div class="MessageShort display-flexAIC mt10-md" style="color: rgb(203, 32, 45);"> <svg width="12" height="12" viewBox="0 0 26 26" class="svg"><g stroke="#cb202d" class="svg-stroke"><circle cx="13" cy="13" r="12.5"> </circle> <path d="M13 6.5V13l3 3"></path></g></svg> <span class="ml5">Low in stock: only 1 left.</span> </div> <div class="CartItem-actionLinks"></div></div> <div class="CartItem-priceAndRemove mlAuto"> <div class="CartItem-oldPrice">2,500 AED</div><div class="CartItem-currentPrice is-discounted">1,625 AED</div> </div> </div>
解决方法
1. 检查iframe嵌套
先确认弹窗是否被嵌套在iframe内,若是需先切换iframe才能定位元素:
// 定位目标iframe(根据实际属性调整定位器) WebElement iframe = driver.findElement(By.xpath("//iframe[contains(@src, 'cart')]")); driver.switchTo().frame(iframe); // 定位弹窗内元素 WebElement cartItem = driver.findElement(By.cssSelector(".CartItem-content")); // 操作完成后切回主文档 driver.switchTo().defaultContent();
2. 添加显式等待
弹窗为动态加载,直接定位会因元素未渲染完成失败,需添加显式等待:
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; // 等待弹窗元素可见,超时10秒 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement cartItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".CartItem-content"))); // 获取弹窗内商品名称示例 String itemName = cartItem.findElement(By.cssSelector(".CartItem-name a")).getText(); System.out.println("弹窗内商品名称:" + itemName);
3. 修正定位器错误
你之前的定位器存在语法问题:
By.className(".CartItem.MiniCartPopup.is-separate"):className方法不能带.,改用cssSelector:By.cssSelector(".CartItem.MiniCartPopup.is-separate")cssSelector误用xpath语法:By.cssSelector("//a[normalize-space()='xxx']")是错误写法,可改用xpath:By.xpath("//a[normalize-space()='Single Arrow Slim Fit Sweatshirt in Cotton Blend']")
4. 模拟鼠标悬浮触发弹窗
若弹窗需鼠标悬浮购物车图标才显示,需先模拟悬浮操作:
import org.openqa.selenium.interactions.Actions; // 定位购物车图标 WebElement cartIcon = driver.findElement(By.cssSelector("a[aria-label='icon-bag']")); // 模拟鼠标悬浮 Actions actions = new Actions(driver); actions.moveToElement(cartIcon).perform(); // 等待弹窗出现后定位元素 WebElement cartItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".CartItem-content")));
内容的提问来源于stack exchange,提问作者Zamir Sultan
相关产品推荐
相关产品推荐

