亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號