首页 > 解决方案 > Selenium - 如何确认提交后更改了带有两个数字的验证码

问题描述

我试图找到有关我的问题的任何帮助,但没有成功。我需要确认验证码(两个数字)在无效提交后被更改。两个数字位于 span 标签中(您可以打开此 URL 并查看https://www.ultimateqa.com/filling-out-forms/)。

脚步:

  1. 打开https://www.ultimateqa.com/filling-out-forms/
  2. 填写右边的表格,但故意输入-1作为加法的结果
  3. 提交表格并确认数字已更改

我解决了前两个步骤,但我用第三步阻止了。有人可以帮我解决这个问题吗?

package zadaci;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class zadatak1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "D:\\DriversSelenium\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://www.ultimateqa.com/filling-out-forms/");

        driver.manage().window().maximize();

        driver.findElement(By.xpath("//*[@id=\"et_pb_contact_name_1\"]")).sendKeys("TestName");

        driver.findElement(By.xpath("//*[@id=\"et_pb_contact_message_1\"]")).sendKeys("TestMessage");

        driver.findElement(By.xpath("/html/body/div[1]/div/div/article/div/div[1]/div/div/div/div[2]/div/div[2]/form/div/div/p/input")).sendKeys("-1");

        driver.findElement(By.xpath("/html/body/div[1]/div/div/article/div/div[1]/div/div/div/div[2]/div/div[2]/form/div/button")).click();

        driver.findElement(By.xpath("/html/body/div[1]/div/div/article/div/div[1]/div/div/div/div[2]/div/div[2]/form/div/div/p/span")).getText();

        driver.close();

            System.out.println("Test script executed successfully.");

    }

}

提前致谢。:)

标签: javaeclipseseleniumselenium-webdriver

解决方案


我在上面添加了这段代码并添加了比较方法。如果可以,请您看看好吗?

package zadaci;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class zadatak1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "D:\\DriversSelenium\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://www.ultimateqa.com/filling-out-forms/");

        driver.manage().window().maximize();

        driver.findElement(By.xpath("//*[@id=\"et_pb_contact_name_1\"]")).sendKeys("TestName");

        driver.findElement(By.xpath("//*[@id=\"et_pb_contact_message_1\"]")).sendKeys("TestMessage");

        driver.findElement(By.xpath("/html/body/div[1]/div/div/article/div/div[1]/div/div/div/div[2]/div/div[2]/form/div/div/p/input")).sendKeys("-1");

        driver.findElement(By.xpath("//span[@class='et_pb_contact_captcha_question']")).getText();

        driver.findElement(By.xpath("/html/body/div[1]/div/div/article/div/div[1]/div/div/div/div[2]/div/div[2]/form/div/button")).click();

        driver.findElement(By.xpath("//span[@class='et_pb_contact_captcha_question']")).getText();

        if("expected String".equals("actual string"))
           {
              System.out.println("Numbers are the same! ");
           }
         else
          {
              System.out.println("Numbers are changed! ");
          }

        driver.close();

            System.out.println("Test script executed successfully.");

    }


    }

谢谢


推荐阅读