【问题标题】:Selenium webdriver not finding add to cart button in AmazonSelenium webdriver 在亚马逊找不到添加到购物车按钮
【发布时间】:2021-08-02 04:02:14
【问题描述】:

选择产品并在另一个页面中打开它后,我无法使用 selenium 在亚马逊点击添加到购物车按钮。

【问题讨论】:

  • 什么是页面 URL?你到底卡在哪里了?

标签: selenium selenium-webdriver select selenium-chromedriver pageobjects


【解决方案1】:

如果您不切换到其他页面,Add to card 选项将不会被定位。 切换到打开的页面,然后尝试点击按钮。

handles = driver.window_handles
driver.switch_to.window(handles[1])
driver.find_element_by_id("add-to-cart-button").click()

如果您这样做了,请分享您尝试过的代码和遇到的错误。

【讨论】:

  • If you are not switching to other page 是什么意思?
  • @cruisepandey - 在亚马逊中点击产品时,产品详细信息会在新窗口中打开。 “添加到购物车”的选项将在第二个窗口中。
  • 不,我不这么认为,至少对我来说不是。这就是为什么我们需要来自 OP 的更多详细信息,而不仅仅是随机给出答案。
  • 我的随机答案也有这个。 If you have done this, share the code you have tried and error you get.
  • 您的声望超过 75,这可能是一条评论。
【解决方案2】:

如果没有您尝试执行的 HTML 页面,则很难准确定位。但是,考虑到这些,我想尝试一下:

  1. 在首页点击产品
  2. 产品在新标签窗口中打开
  3. 点击加入购物车

PS:我将使用 Java 编写它,因为您没有提到所使用的语言。其他人也会类似

在首页点击产品

List <Webelement> productCheck = driver.findElements(By.xpath("<xpath of the product>"))
if (productCheck.size() == 0){
// do something
else {
driver.findElement(By.xpath("<xpath of the product>")).click();
}

产品在新标签窗口中打开点击添加到购物车

String parent = driver.getWindowHandle();
List<String> windows = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(windows.get(1));

// Inside the tab window

List <WebElement> addtoCartButton = driver.findElements(By.xpath("<xpath of Add to cart button>"));
if (addtoCartButton.size() == 0 ) {
// do something
else {
driver.findElement(By.xpath("<xpath of Add to cart button>")).click();
}


// do whatever you want in the new tab and if you want to switch back then:

driver.switchTo().window(parent);

【讨论】:

  • 是的,非常感谢您
猜你喜欢
  • 2012-08-27
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多