在 Anand 和 Prophet 給出答案后,我對代碼進行了更改,但現在無論是否創建帳戶,它都無法驗證測驗結果。理想情況下,它應該驗證在提供所有必需資訊后是否創建了帳戶。我不確定哪里出了問題,請同樣幫助我。
package Seleniumtesting;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium {
ChromeDriver driver;
String url ="https://login.mailchimp.com/signup/";
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
TimeUnit.SECONDS.sleep(2);
driver.get(url);
String urlFromWebpage = driver.getCurrentUrl();
if(urlFromWebpage.equals("https://login.mailchimp.com/signup/")) {
System.out.println("PASS");
}
else {
System.out.println("FAIL");
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public void signup(){
try {
WebElement createAccountHeading = driver.findElement(By.xpath("//span[text()='Create an account or ']"));
if(createAccountHeading.isDisplayed()) {
System.out.println("PASS");
}else
System.out.println("FAIL");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("username")).sendKeys("Testvina1243");
driver.findElement(By.name("password")).sendKeys("Test123@");
TimeUnit.SECONDS.sleep(2);
driver.findElement(By.name("marketing_newsletter")).click();
TimeUnit.SECONDS.sleep(2);
//driver.findElement(By.xpath("//button[@id='create-account']")).click();
driver.findElement(By.xpath("//*[@id=\"create-account\"]")).click();
TimeUnit.SECONDS.sleep(2);
String u = driver.getCurrentUrl();
System.out.println("URL: " u);
/*if(u.equalsIgnoreCase("https://login.mailchimp.com/signup/success/"))
{
System.out.println("PASS !! Account created successfully");
}
else
{
System.out.println("FAIL !! It might have not met the criteria");
}*/
driver.close();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
Selenium mc = new Selenium();
mc.invokeBrowser();
mc.signup();
}
}
uj5u.com熱心網友回復:
這可以用于注冊:
driver.find_element(By. ID, "create-account").click()
需要注意的是,只有在滿足密碼條件時才會啟用它,對于 mailchimp,至少(根據網站):1 個小字符,1 個大字符,1 個數字,1 個特殊字符,最小密碼長度為 8 .
我看到您將密碼用作“Test123”,這不會啟用該按鈕,因為未滿足設定的標準。請檢查網站密碼輸入框下方給出的密碼規則。
更新:根據您的最新評論,我在這里更新。我看到,當我測驗時,它會重定向到另一個頁面,它要求電子郵件確認。
https://login.mailchimp.com/signup/success/?username=Test1243&userId=168758830&loginId=181489470
現在,里面有/success/。而且我想您正在使用此頁面來斷言您的行為是成功的;在這種情況下,我會說你.equals在你的代碼中使用了它,它失敗了,因為 url 比你正在尋找的更多,所以它不應該是.equals,但它應該是類似.contains或類似的東西(我不知道Java使用什么,所以請搜索那個等效的關鍵字)
更新(@vicky 每個請求的 Java 代碼編輯)
public void signup(){
try {
WebElement createAccountHeading = driver.findElement(By.xpath("//span[text()='Create an account or ']"));
if(createAccountHeading.isDisplayed()) {
System.out.println("PASS");
}else
System.out.println("FAIL");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("username")).sendKeys("Testvina1243");
driver.findElement(By.name("password")).sendKeys("Test123@");
TimeUnit.SECONDS.sleep(2);
// code edit by @anandgautam
driver.findElement(By.xpath("//div[@id='onetrust-close-btn-container")).click();
TimeUnit.SECONDS.sleep(1);
driver.findElement(By.name("marketing_newsletter")).click();
TimeUnit.SECONDS.sleep(5);
// end of code edit by @anandgautam
//driver.findElement(By.xpath("//button[@id='create-account']")).click();
driver.findElement(By.xpath("//*[@id=\"create-account\"]")).click();
TimeUnit.SECONDS.sleep(2);
String u = driver.getCurrentUrl();
System.out.println("URL: " u);
/*if(u.equalsIgnoreCase("https://login.mailchimp.com/signup/success/"))
{
System.out.println("PASS !! Account created successfully");
}
else
{
System.out.println("FAIL !! It might have not met the criteria");
}*/
driver.close();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
uj5u.com熱心網友回復:
您的密碼缺少特殊字符。在您用有效資料填寫所有欄位之前,該頁面上不會出現“注冊”按鈕。
因此,如果您將密碼從Test123fe更改為 fe,Test123$您將能夠看到、找到并單擊帶有此代碼的 Sing Up 按鈕:
driver.findElement(By.xpath("//button[@id='create-account']")).click();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409796.html
標籤:
