-1

我有这个代码来查找元素(从购物车中删除产品),在我的情况下,我有一个以上的元素,所以我需要一个循环从购物车中一一删除产品,这是我的代码,但它不起作用:while(self.driver.find_elements_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]')): self.driver.find_elements_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]').click()

4

1 回答 1

1

您的代码不起作用,因为您试图单击元素数组 ( self.driver.find_elements_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]').click())。可能的解决方案是find_element_by_xpath在单击时使用(不带“s”)。

或者

你应该能够做到这一点:

# get all remove item buttons
removeButtons = self.driver.find_elements_by_xpath('//*[@data-testid="RemoveProductBtn_btn"]')
# loop over each element and click on it
for removeButton in removeButtons:
  removeButton.click()
于 2022-02-18T18:51:17.033 回答