当我的构造函数Select(By)未定义时,如何从Selenium POM中选择下拉列表?

问题描述 投票:0回答:2

我目前正在尝试从商店页面中选择一个下拉列表,该页面与我想要在Selenium上使用POM购买的衬衫数量相匹配。我已按照此问题的类似答案列出的说明进行操作,但它似乎对我不起作用。

这是我到目前为止在存储页面对象的Java文件中所做的事情:

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class TTPStorePage {

    WebDriver driver;

    public TTPStorePage(WebDriver driver) {
        this.driver = driver;
    }

    By size= By.id("size");
    By reset= By.className("reset_variations");
    By quantity= By.id("quantity_5cb788738ee07");
    By submit=By.cssSelector("button[type='submit']");
    By remove=By.xpath("//a[contains(@data-gtm4wp_product_id,'TS-TTP']");
    By contents=By.className("cart-contents");

    // Right here.
    public WebElement selectSize(int index) {
        Select drop = new Select(size);
        drop.selectByIndex(index);
    }

    public WebElement resetItems() {
        return driver.findElement(reset);
    }

    public WebElement quantityItem() {
        return driver.findElement(quantity);
    }

    public WebElement submitButton() {
        return driver.findElement(submit);
    }

    public WebElement removeItem() {
        return driver.findElement(remove);
    }

    public WebElement cartContents() {
        return driver.findElement(contents);
    }

}

这是我自己运行测试用例的文件:

package SimpleProgrammer;

import java.io.IOException;
import org.testng.annotations.Test;
import resources.Base;
import pageObjects.TTPProductPage;
import pageObjects.TTPStorePage;

public class PurchaseApplication extends Base {

    @Test
    public void BuyItem() throws IOException {
        driver=initializeDriver();
        driver.get("https://simpleprogrammer.com/store/products/trust-the-process-t-shirt/");

        TTPProductPage pp= new TTPProductPage(driver);
        pp.TTPButton().click();
        TTPStorePage sp = new TTPStorePage(driver);
        // The problem child.
        sp.selectSize(2);
    }

}
java eclipse selenium pageobjects
2个回答
3
投票

你有

Select drop = new Select(By.id("size"));

但我认为它应该是

Select drop = new Select(driver.findElement(By.id("size")));

0
投票

你需要尝试这个: -

WebDriver的支持类称为“Select”,它提供了与select选项交互的有用方法。用户可以对选择下拉列表执行操作,也可以取消选择操作。

Select drop = new Select(driver.findElement(By.id("size")));

欲了解更多信息,请访问this链接。

© www.soinside.com 2019 - 2024. All rights reserved.