【发布时间】:2021-08-02 04:02:14
【问题描述】:
选择产品并在另一个页面中打开它后,我无法使用 selenium 在亚马逊点击添加到购物车按钮。
【问题讨论】:
-
什么是页面 URL?你到底卡在哪里了?
标签: selenium selenium-webdriver select selenium-chromedriver pageobjects
选择产品并在另一个页面中打开它后,我无法使用 selenium 在亚马逊点击添加到购物车按钮。
【问题讨论】:
标签: selenium selenium-webdriver select selenium-chromedriver pageobjects
如果您不切换到其他页面,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 是什么意思?
If you have done this, share the code you have tried and error you get.
如果没有您尝试执行的 HTML 页面,则很难准确定位。但是,考虑到这些,我想尝试一下:
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);
【讨论】: