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

How can I select dropdown from Selenium POM when my constructor Select(By) is undefined?

我目前正在尝试 select 商店页面的下拉列表,它与我想在 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);
    }

}

你有

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

但我认为应该是

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

你需要试试这个:-

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

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

有关详细信息,请查看 this link。