java - 需要在字段中输入值,检查结果,然后在 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();
}
}
解决方案
您错误地声明了班级。它应该是
public class Sample{
//Then the stuff you want to do.
}
要访问此类,您需要创建外部类对象,并且它们内部像这样。
Outer d=new Outer();
d.Sample obj=new d.Sample();
但是,当您为示例类声明成员静态时,您可以使用类名来调用它们。
推荐阅读
- twilio - 使用 Twillio Studio 时,contact-channel-address 是带有 +1 的自动前缀 - 拨打电话
- python - Pandas:合并 2 个数据帧后重新排列列而不重新索引
- python - 如何解决将文件上传到 Google Cloud Storage 时的内存泄漏问题?
- python - 有没有办法为石墨烯输入类型指定类型转换器?
- whatsapp - 使用 gupshup.io 平台实现 Whatsapp 业务 API
- angular-cli - 可以使用“ng generate application”为子目录生成应用程序吗?
- c++ - Vtk 图表在 QT 中中断,“没有为 'vtkContextDevice2D 找到覆盖”
- keycloak - Angular项目使用keycloak管理访问权限
- html - 需要一个在某个元素第一次出现之前找到特定类型的所有元素的 xpath
- php - 我想在一段中找到一个单词的第一个字母的strpos