如果变量值在j = <10之间,则分页显示错误“对不起,用户已经...” 然后我尝试为变量k分配另一个随机值,然后再次填写表格。
仅当出现错误时,我才想执行driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()
步骤。否则,仅打印成功消息。
即使我收到成功消息,这里的问题仍然是由于driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()
而导致找不到元素错误。
我也尝试实现boolean Sorry_Error = driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed();
,但我认为这是相似的逻辑,只是实现方式不同。如果没有错误,则循环应继续执行else块,但将其插入.isDisplayed
xpath并抛出错误。
处理用户已经存在的错误不是正确的方法,还是有其他逻辑?
package com.self.learn;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class testandquiz {
public static WebDriverWait wait;
@Test
public void Simple_Test() throws InterruptedException {
System.setProperty("webdriver.chrome.silentOutput", "true");
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
WebDriver driver = new ChromeDriver();
wait = new WebDriverWait(driver, 10);
driver.manage().deleteAllCookies();
driver.get("https://www.testandquiz.com/user-registration");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
((JavascriptExecutor) driver).executeScript("scroll(0,300)");
WebElement Fname = driver.findElement(By.id("first_name"));
WebElement Lname = driver.findElement(By.id("last_name"));
WebElement User_name = driver.findElement(By.id("user_name"));
WebElement email_address = driver.findElement(By.id("email_address"));
WebElement password = driver.findElement(By.id("password"));
WebElement re_password = driver.findElement(By.id("re_password"));
WebElement city = driver.findElement(By.id("city"));
WebElement state = driver.findElement(By.id("state"));
WebElement country = driver.findElement(By.id("country"));
WebElement pin_code = driver.findElement(By.id("pin_code"));
WebElement contact_no = driver.findElement(By.id("contact_no"));
Random randomGenerator = new Random();
int j = 10;
int k = 99999;
int i = randomGenerator.nextInt(j);
Fname.sendKeys("Test" + i + "");
Lname.sendKeys("Test" + i + "");
User_name.sendKeys("Test" + i + "");
email_address.sendKeys("Test" + i + "");
password.sendKeys("Test" + i + "");
re_password.sendKeys("Test" + i + "");
city.sendKeys("Test" + i + "");
state.sendKeys("Test" + i + "");
country.sendKeys("Test" + i + "");
pin_code.sendKeys("Test" + i + "");
contact_no.sendKeys("1234567890");
driver.findElement(By.xpath("//button[contains(text(),'Sign Up')]")).click();
if (driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()) {
WebElement Fname1 = driver.findElement(By.id("first_name"));
WebElement Lname1 = driver.findElement(By.id("last_name"));
WebElement User_name1 = driver.findElement(By.id("user_name"));
WebElement email_address1 = driver.findElement(By.id("email_address"));
WebElement password1 = driver.findElement(By.id("password"));
WebElement re_password1 = driver.findElement(By.id("re_password"));
WebElement city1 = driver.findElement(By.id("city"));
WebElement state1 = driver.findElement(By.id("state"));
WebElement country1 = driver.findElement(By.id("country"));
WebElement pin_code1 = driver.findElement(By.id("pin_code"));
WebElement contact_no1 = driver.findElement(By.id("contact_no"));
Fname1.clear();
Fname1.sendKeys("Test" + k + "");
Lname1.clear();
Lname1.sendKeys("Test" + k + "");
User_name1.clear();
User_name1.sendKeys("Test" + k + "");
email_address1.clear();
email_address1.sendKeys("Test" + k + "");
password1.clear();
password1.sendKeys("Test" + k + "");
re_password1.clear();
re_password1.sendKeys("Test" + k + "");
city1.clear();
city1.sendKeys("Test" + k + "");
state1.clear();
state1.sendKeys("Test" + k + "");
country1.clear();
country1.sendKeys("Test" + k + "");
pin_code1.clear();
pin_code1.sendKeys("Test" + k + "");
contact_no1.clear();
contact_no1.sendKeys("1234567890");
driver.findElement(By.xpath("//button[contains(text(),'Sign Up')]")).click();
}
else {
String Message = driver.findElement(By.xpath("//div[contains(text(),'verification mail')]")).getText();
System.out.println(Message);
}
}
}
我不知道如何实现这种逻辑。
答案 0 :(得分:0)
您在这里不一定需要等待...取决于JavaScript是否更新DOM。如果是在标准页面加载之后,则只需使用:
List<WebElement> elements = driver.findElements(By.xpath("//*[contains(text(), 'Sorry')]");
if (elements.size() > 0)
{
// sorry found
}
else
{
// sorry not found
}
否则请尝试使用try / catch:
try {
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Sorry')]"));
// sorry found
}
catch (Exception ex)
{
//sorry not found
}
对错误文本使用相同的逻辑。