我目前正在尝试从商店页面中选择一个下拉列表,该下拉列表与我要在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);
}
}
答案 0 :(得分:3)
你有
Select drop = new Select(By.id("size"));
但是我认为应该是
Select drop = new Select(driver.findElement(By.id("size")));
答案 1 :(得分:0)