简体   繁体   English

无法从网站获取错误消息并使用 Selenium(java) 在 Eclipse 控制台中打印

[英]Unable to Grab error message from website and print in Eclipse console using Selenium(java)

I am trying to get the Error message printed in SalesForce website for entering incorrect username and password我正在尝试在 SalesForce 网站上打印错误消息以输入错误的用户名和密码

package today;

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

public class Gmail {

public static void main(String[] args) {
 System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");      WebDriver mail=new ChromeDriver();
mail.get("https://login.salesforce.com/?locale=in");


mail.findElement(By.cssSelector("#username")).sendKeys("judinkp@gmail.com");
mail.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("23232");

mail.findElement(By.id("Login")).click();

System.out.println(mail.findElement(By.xpath("//*[@id='error']")).getText());
    }
}

My Script runs till it clicks on Login but the Error message printed in website is not getting printed in my console , I am getting below error message我的脚本一直运行,直到它点击Login但网站中打印的错误消息没有打印在我的控制台中,我收到以下错误消息

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='loginError']"}
  (Session info: chrome=80.0.3987.149)

The Xpath is Browser given Xpath. Xpath 是浏览器给定的 Xpath。

Website Link:https: //login.salesforce.com/?locale=in网站链接:https: //login.salesforce.com/?locale=in

Can you please try below solution:您能否尝试以下解决方案:

import org.openqa.selenium.By;
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;

public class LoginScreen {

    public static void main(String[] args) {

        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");  



        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.navigate().to("https://login.salesforce.com/?locale=in");
        driver.get("https://login.salesforce.com/?locale=in");
        driver.findElement(By.cssSelector("#username")).sendKeys("judinkp@gmail.com");
        driver.findElement(By.id("password")).sendKeys("23232");
        driver.findElement(By.id("Login")).click();
        WebDriverWait wait = new WebDriverWait(driver,20);
        WebElement error= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error")));  
        System.out.println(error.getText());
    }
}

I had the same issue with CSS and Xpath locators.我对 CSS 和 Xpath 定位器有同样的问题。

Try with System.out.println(mail.findElement(By.id("error")).getText());尝试使用 System.out.println(mail.findElement(By.id("error")).getText()); it should print out in the console.它应该在控制台中打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM