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

标签 java selenium xpath

我试图在 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());
    }
}

我的脚本一直运行,直到单击登录,但网站中打印的错误消息没有打印在我的控制台中,我收到以下错误消息

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)

Xpath 是浏览器给定的 Xpath。

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

最佳答案

您可以尝试以下解决方案吗:

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());
    }
}

关于java - 无法使用 Selenium(java) 从网站抓取错误消息并在 Eclipse 控制台中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915600/

相关文章:

javascript - 在selenium中执行滚动到 View 脚本时出现空指针异常

xml - 使用XSL将XML树转换为平面节点列表

xpath - 我正在尝试获取一个值的 Xpath 但嵌套条件出错

java - 如何在Java springs中将字符串数组传递给@PathVariable

java - 使用 Lombok 的显式构造函数?

java - MultiKeyMap 与具有映射值的映射

selenium - ExpectedConditions.invisibilityOfElementLocated 不起作用

selenium - webdriver-manager 更新,错误 : unable to get local issuer certificate

java - 如何在 Java SimpleXML 中序列化没有节点的封闭标签

xpath - Katalon-无法读取SVG