【发布时间】:2020-07-28 16:55:49
【问题描述】:
HTML:
“加入购物车”对象
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="ProductPrimaryCTA-cta_add_to_cart_button" type="button"><span class="button-wrapper"><span class="spin-button-children">Add to cart</span></span></button>
“进货提醒”对象
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="cta_oos_button" aria-expanded="false" aria-label="get in stock alert" role="button" tabindex="0" type="button"><span class="button-wrapper"><span class="spin-button-children">Get in-stock alert</span></span></button>
我不想点击“加入购物车”。我只想将定位器信息存储在 WebElement“addToCart”中。因为对象具有非常相同的属性,所以我选择了这个独特的属性“data-tl-id”但是没有用。
网络驱动程序
WebElement addToCart = driver.findElement(By.xpath("//button[@data-tl-id='ProductPrimaryCTA-cta_add_to_cart_button']"));
下面对我有用,但上面两个对象都有相同的类名,所以我不能使用类名。
WebElement addToCart = driver.findElement(By.xpath("//span[@class='button-wrapper']"));
我也尝试了以下方法,但没有一个对我有用。
WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
WebElement addToCart = driver.findElement(By.xpath("//span[contains(@text,'Add to cart')]"));
我收到错误“没有这样的元素:无法找到元素:”。如何定位“加入购物车”对象?
【问题讨论】:
标签: selenium