如何在容器内循环以选择硒值?

时间:2018-11-29 23:33:13

标签: java loops selenium automation containers

我有一个产品页面,该页面在容器内具有尺寸,我尝试列出元素并按文本获取尺寸,但列表始终返回零,我尝试了父级和子级的xpath,并且得到相同的错误,我列出尺寸并选择特定尺寸?

class pistons
    {
       public static int piston_code() {
           Random head = new Random();
           int beta = head.Next(100, 999);
           Console.WriteLine("piston serial number " + beta);
           return beta; //after that how i can access returned value?
                       // how to assign it to other variable out of "piston_code"?
      }

Product page

3 个答案:

答案 0 :(得分:0)

在我看来,合适的选择器应该是:

actions.driver.findElements(By.cssSelector(".SizeSelection-option"))

答案 1 :(得分:0)

尝试以下选项

List<WebElement> sizesList = actions.driver.findElements(By.xpath("//[@class='SelectSizeLoactor']"));
List<WebElement> sizesList = actions.driver.findElements(By.cssSelector(".SelectSizeLoactor"));

答案 2 :(得分:0)

我找到了一个快速解决方案,我将一部分xpath与text()一起使用,并在稍后传递该文本的值,然后添加了最后一个xpath,它起作用了!

String SelectSizeLoactor = "//button[text()='"

public void chooseSize(String size) {
    String selectedSize;
    WebElement sizeLocator = actions.driver.findElement(By.xpath(SelectSizeLoactor+size.toUpperCase()+"']"));
    try {
            if (sizeLocator.getText().toUpperCase().contains(size.toUpperCase()));
            {
                selectedSize = sizeLocator.getText();
                sizeLocator.click();
                assertTrue(selectedSize.equals(size));
            }

    } catch (Exception e) {
        Assert.fail("Couldn't select size cause of " + e.getMessage());

    }
}