【问题标题】:Out of stock validation in Selenium webdriverSelenium webdriver 中的缺货验证
【发布时间】:2017-06-14 12:28:35
【问题描述】:

我是编程和 selenium webdriver 的新手,我正在尝试在网站上执行以下操作,请您帮忙

步骤 1。从https://www.o2.co.uk/shop/smartwatches/#sort=content.sorting.featured&page=1中随机选择一个项目

点击后会导航到https://www.o2.co.uk/shop/smartwatches/samsung/gear-s2/#contractType=nonconnected

这里我需要检查商品是有货还是缺货

如果缺货,我需要回到Step1

然后选择下一项。

任何人都可以帮助使用 java 中的函数吗?

【问题讨论】:

标签: java selenium webdriver


【解决方案1】:

试试这个。

// Step1: Go to watch page.
driver.get("https://www.o2.co.uk/shop/smartwatches/#sort=content.sorting.featured&page=1");

// Step2: Click on selected watch
WebElement element = driver.findElement(By.id("mywatch"));
driver.click(element);

// Step3: Get the status of watch
String item_status = driver.findElement(By.className("status-info")).getText();

//Step4: Navigate back if selected item is 'Out of Stock'
if(status.equals("Out of Stock")){
    driver.navigate().back();
}

【讨论】:

    【解决方案2】:

    您可以检查如下情况:

    // After product details page
    
    List<WebElement> outOfStockItem = driver.findElements(By.xpath("//p[@class='delivery-information']/span[contains(.,'Out of stock')]"));
    
    if(outOfStockItem.size()>0)
    {
        driver.navigate().back();
    }
    else
    {
            // perform your further actions here
    }
    

    说明:一旦您的页面登陆产品详细信息页面,例如https://www.o2.co.uk/shop/accessories/kitsound/mini-buddy-speaker/#contractType=nonconnected 在此 URL 上。它将检查是否存在任何缺货元素。如果存在,那么它会将页面导航到上一页。在其他情况下,您可以管理您的代码以进行进一步处理

    或者您可以在选择产品颜色后编写此代码,因为在给定的网站上,某些颜色的某些产品缺货。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 2022-08-19
      • 2013-09-25
      • 2013-10-18
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      相关资源
      最近更新 更多