为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Selenium POM 中增加?

如何在 Selenium POM 中增加?

波斯汪 2022-10-12 15:55:10
我试图通过执行 for 循环在输入标签上增加两次来选择 Selenium POM 中相同项目的两个数量,但我的解决方案似乎不起作用。如何使用 POM 递增两次?这是我存储页面对象的文件:package pageObjects;import org.openqa.selenium.By;import org.openqa.selenium.Keys;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 quantity= By.id("quantity_5cb788738ee07");    By reset= By.className("reset_variations");    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");    public void selectSize(int index) {        Select drop = new Select(driver.findElement(size));        drop.selectByIndex(index);    }    // The problem child.    public void quantityItem() {        for(int i=0;i<2;i++) {            driver.findElement(quantity).sendKeys(Keys.ARROW_UP);        }    }    public WebElement resetItems() {        return driver.findElement(reset);    }    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;    }}
查看完整描述

2 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

它是 INPUT 字段,只需先清除输入字段,然后为 sendKeys 提供值。请取唯一的名称 attr,您的 ID attrs 不是唯一的。


WebElement ele=driver.findElement(By.name("quantity"));

ele.clear();

ele.sendKeys("2");


查看完整回答
反对 回复 2022-10-12
?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

我查看了您尝试访问的网页。您尝试TTPStorePage通过定位器在类中访问的微调器quantity具有动态 ID。每次加载页面时它都会更改。您必须更改定位器策略。


尝试使用以下定位器之一作为数量。


CSS选择器:


By quantity = By.cssSelector("div.quantity > input");

XPath:


By quantity = By.xpath("//div[@class='quantity']/input");

此外,quantityItem您不需要 for 循环的 in 方法,因为您可以将值直接设置为您想要的值,sendKeys因为它是一个input元素。


尝试这个


public void quantityItem() {

    driver.findElement(quantity).clear();

    driver.findElement(quantity).sendKeys("3");

    //pressing up arrow twice would make the spinner value 3

}



查看完整回答
反对 回复 2022-10-12
  • 2 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信