Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何增加硒POM?_Java_Eclipse_Selenium - Fatal编程技术网

Java 如何增加硒POM?

Java 如何增加硒POM?,java,eclipse,selenium,Java,Eclipse,Selenium,我试图在Selenium POM中选择两个数量相同的项,方法是在输入标记上执行for循环以增加两次,但我的解决方案似乎不起作用。如何使用POM增加两次 以下是存储页面对象的文件: package pageObjects; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElemen

我试图在Selenium POM中选择两个数量相同的项,方法是在输入标记上执行for循环以增加两次,但我的解决方案似乎不起作用。如何使用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);
    }

}

这是输入字段,请先清除输入字段,然后为sendKeys提供值。请使用唯一的名称attr。您的ID attr不是唯一的

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

我已查找了您试图访问的网页。您试图通过定位器数量在TTPStorePageclass中访问的微调器具有动态id。它在每次加载页面时都会更改。你必须改变你的定位策略

尝试对数量使用以下定位器之一

Css选择器:

XPath:

另外,方法中的quantityItem不需要for循环,因为它是一个输入元素,所以可以通过sendKeys直接将值设置为所需的值

试试这个


“数量”字段只是一个输入。为什么不直接将该值设置为2?在我所在的网站上,如果我发送了键“2”,则该值会显示为“12”。输入字段上已存在“1”。我正在自动化的网站是这样一个:Do a.clear then.sendKeys。我得到了一个NoTouchElementException,这很奇怪,因为我输入了正确的ID。你有等待集吗?如果没有,请添加waitForVisible。
WebElement ele=driver.findElement(By.name("quantity"));
ele.clear();
ele.sendKeys("2");
By quantity = By.cssSelector("div.quantity > input");
By quantity = By.xpath("//div[@class='quantity']/input");
public void quantityItem() {
    driver.findElement(quantity).clear();
    driver.findElement(quantity).sendKeys("3");
    //pressing up arrow twice would make the spinner value 3
}