英文:
How to solve this problem using if-else loop in java selenium?
问题
如果变量值在 j <= 10 之间,则显示错误页面 "抱歉!用户已经……",然后我尝试给变量 k 分配另一个随机值,并重新填写表单。
只有当出现错误时,我才想执行 driver.findElement(By.xpath("//*[contains(text(),'抱歉!')]")).isDisplayed()
步骤。否则,只需打印成功消息。
问题在于,即使我收到成功消息,由于 driver.findElement(By.xpath("//*[contains(text(),'抱歉!')]")).isDisplayed()
,我仍然收到找不到元素的错误。
我还尝试了实现 boolean Sorry_Error = driver.findElement(By.xpath("//*[contains(text(),'抱歉!')]")).isDisplayed();
但我认为这是相似的逻辑,只是实现方式不同。如果没有错误,循环应该进入 else 块,但它却在搜索 .isDisplayed
的 xpath 并抛出错误。
处理用户已经存在的错误难道不是正确的方式,还是有其他逻辑?
(代码部分未翻译)
英文:
If variable value between j =<10 then page through error "Sorry ! user already..."
and then I try to assign another random value to variable k and fill up form again.
I want to execute driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()
step only if I get error. Else just print success message.
Problem here is even if I get success message I'm getting element not found error due to driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed()
.
I also tried implementing boolean Sorry_Error = driver.findElement(By.xpath("//*[contains(text(),'Sorry ! ')]")).isDisplayed();
but I think it is similar logic just different way of implementation. if there is no error then loop should go for else block but insted it searching .isDisplayed
xpath and throwing error.
Isn't it correct way to handle user already exist error or is there any other logic.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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);
}
}
}
<!-- end snippet -->
I don't know how to implement this logic.
答案1
得分: 0
你这里不一定需要等待... 这取决于 JavaScript 是否更新了 DOM。如果是在标准页面加载之后,那么只需使用:
List<WebElement> elements = driver.findElements(By.xpath("//*[contains(text(), 'Sorry')]"));
if (elements.size() > 0) {
// 找到了抱歉提示
} else {
// 没有找到抱歉提示
}
否则,可以使用带有等待的 try/catch 块:
try {
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Sorry')]")));
// 找到了抱歉提示
} catch (Exception ex) {
// 没有找到抱歉提示
}
对错误文本使用相同的逻辑。
英文:
You won't necessarily need a wait here... depends on if javascript updates the DOM. If it's after a standard page load, then just use:
List<WebElement> elements = driver.findElements(By.xpath("//*[contains(text(), 'Sorry')]");
if (elements.size() > 0)
{
// sorry found
}
else
{
// sorry not found
}
Else use try/catch with a wait:
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
}
Use same logic for error text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论