Selenium WebDriver:无法找到Amazon.com的“添加到购物车”按钮

时间:2017-03-06 19:18:12

标签: selenium xpath selenium-webdriver

我尝试了几件事,但仍无法找到按钮。它不在框架内。

driver.findElement(By.xpath(".//input[@id='add-to-cart-button'")).click();

driver.findElement(By.xpath(".//input[@id='add-to-cart-button' and 
@name='submit.add-to-cart']")).click();

driver.findElement(By.cssSelector("#add-to-cart-button")).click();

尝试添加WebDriverWait

WebDriverWait wait= new WebDriverWait(driver, 20);
  element=wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(XPATH_ADD_TO_CART))));

以下是相关的html

<input id="add-to-cart-button" 
  name="submit.add-to-cart" 
  title="Add to Shopping Cart" 
  data-hover="Select 
    <b>__dims__</b> from the left<br> to add to Shopping Cart" 
  class="a-button-input" 
  type="submit" 
  value="Add to Cart" 
  aria-labelledby="submit.add-to-cart-announce">

请帮助,我在这里缺少什么?TIA

2 个答案:

答案 0 :(得分:0)

您错过了结束]括号。

变化

driver.findElement(By.xpath(".//input[@id='add-to-cart-button'")).click();

driver.findElement(By.xpath(".//input[@id='add-to-cart-button']")).click();

答案 1 :(得分:0)

Python 2.7

通过xpath:

driver.find_element_by_xpath("//input[@class='a-button-input']").click()
driver.find_element_by_xpath("//input[@id='add-to-cart-button']").click()

最简单的方法:

driver.find_element_by_id('add-to-cart-button')