首页 > 解决方案 > 需要在字段中输入值,检查结果,然后在 Selenium Java 中添加一个字符

问题描述

这是我第一次编码,所以请原谅我的无知。

我有以下 Selenium 代码,用于从餐厅在线订购。最后,它是将一个值放入一个字段,检查并打印结果,然后我需要做的是更改原始输入,然后再做一次。所以我需要一个循环。我认为。

package Test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.Random;
import java.util.Scanner;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Test {

public static void main(String[] args) throws Exception {


    System.setProperty("webdriver.chrome.driver", "C:\\Users\\testuser\\Desktop\\Eclipse\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        Actions action = new Actions(driver);

        driver.get("onlineorder.com");
        //driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);

        WebElement startorder = driver.findElement(By.cssSelector("#startOrder_148"));
        startorder.click();
        Thread.sleep(4500);
        action.sendKeys(Keys.RETURN);
        Thread.sleep(3000);

        WebElement selectfood = driver.findElement(By.xpath("//a[contains(text(),\"NEW! Steak Po'boy (670 cal)\")]"));
        selectfood.click();
        Thread.sleep(4000);

        WebElement additem = driver.findElement(By.cssSelector("#addItemToOrder"));
        additem.click();
        Thread.sleep(5000);

        WebElement checkout = driver.findElement(By.xpath("//a[contains(text(),'Checkout')]"));
        checkout.click();
        Thread.sleep(5000);

        WebElement loginbutton = driver.findElement(By.cssSelector("#logonCheckoutBtn"));
        loginbutton.click();
        Thread.sleep(3500);

        //WebElement click5 = driver.findElement(By.xpath("//input[@id='email']"));
        //action.click(click5).perform();
        driver.findElement(By.xpath("//input[@id='email']")).sendKeys("email@gmail.com");
        driver.findElement(By.cssSelector("#loginPassword")).sendKeys("password");

        WebElement loginbutton2 = driver.findElement(By.cssSelector("#loginButton"));
        loginbutton2.click();
        Thread.sleep(5500);

        WebElement paymenttype = driver.findElement(By.id("selectPaymentType"));
        Select payment=new Select(paymenttype);
        payment.selectByIndex(3);
        driver.findElement(By.id("numberchecker")).sendKeys("1000");
        WebElement checkbalance = driver.findElement(By.xpath("//div[@class='clearfix ng-scope']//div[@class='clearfix']//a[@class='numberchecker btn'][contains(text(),'Check balance')]"));
        checkbalance.click();
        Thread.sleep(1500);

        for(WebElement link:driver.findElements(By.xpath("//span[@class='popup_message ng-binding']")))

        {
            System.out.println(link.getText());

        }

        WebElement okaybalance = driver.findElement(By.cssSelector("#btnPopupOk"));
        okaybalance.click();

直到这里一切正常。我需要做的是返回并将原始输入值(1000)从 1 更改为 1001。我得到的 getInputNumber 错误是“AnnotationName expected after this token”。此外,“addnumber”类的名称给出错误“非法修饰符,只允许抽象或最终”。“类”给出错误“语法错误,@预期”。

        public class addnumber() {
                private static float inputNumber= 1000f;

                public static float getInputNumber() {
                    return inputNumber+ 1.0f;
                }
            }


        Thread.sleep(4000);

        //driver.quit();
}

}

标签: javaselenium

解决方案


您错误地声明了班级。它应该是

public class Sample{

//Then the stuff you want to do.


}

要访问此类,您需要创建外部类对象,并且它们内部像这样。

Outer d=new Outer();
d.Sample obj=new d.Sample();

但是,当您为示例类声明成员静态时,您可以使用类名来调用它们。


推荐阅读