简体   繁体   English

尝试使用 Java 在 Selenium 中运行测试用例时,线程“主”java.lang.NullPointerException 出现异常

[英]Exception in thread "main" java.lang.NullPointerException when trying to run a test case in Selenium with Java

This is the Java code I have:这是我拥有的 Java 代码:

package test;

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 java.util.concurrent.TimeUnit;

public class FirstSeleniumTest {

public static WebDriver driver;

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver","C:\\Users\\35196\\IdeaProjects\\selenium test\\SeleniumTest\\lib\\seleniumjars\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.navigate().to("https://foodplanhealth.herokuapp.com/login");

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

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    WebElement root1 = driver.findElement(By.cssSelector("my-app"));
    WebElement shadow_root1 = expand_shadow_element(root1);

    WebElement root2 = shadow_root1.findElement(By.cssSelector("mwc-drawer"));
    WebElement shadow_root2= expand_shadow_element(root2);

    WebElement root3 = shadow_root2.findElement(By.name("appContent"));
    WebElement shadow_root3 = expand_shadow_element(root3);

    WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content"));
    WebElement shadow_root4 = expand_shadow_element(root4);

    WebElement root5 = shadow_root4.findElement(By.cssSelector("div#container"));
    WebElement shadow_root5 = expand_shadow_element(root5);

    WebElement root6 = shadow_root5.findElement(By.cssSelector("login-container"));
    WebElement shadow_root6 = expand_shadow_element(root6);

    WebElement root7 = shadow_root6.findElement(By.cssSelector("mwc-textfield"));
    WebElement shadow_root7 = expand_shadow_element(root7);

    WebElement root8 = shadow_root7.findElement(By.cssSelector("mdc-text-field__ripple"));
    WebElement shadow_root8 = expand_shadow_element(root8);

    WebElement login = shadow_root8.findElement(By.cssSelector("mdc-text-field__input"));

    String js = "arguments[0].setAttribute('value','random@email.com')";
    ((JavascriptExecutor) driver).executeScript(js, login);

}

public static WebElement expand_shadow_element(WebElement element)
{
    return (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
}
}

The code runs fine up until line 35 WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content"));代码运行良好,直到第 35 行WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); where i get this error我在哪里得到这个错误

Exception in thread "main" java.lang.NullPointerException at test.FirstSeleniumTest.main(FirstSeleniumTest.java:35) test.FirstSeleniumTest.main(FirstSeleniumTest.java:35) 线程“主”中的异常 java.lang.NullPointerException

I know the code in its entirety is not correct and incomplete, but I can't move forward because of this error.我知道整个代码不正确且不完整,但由于此错误,我无法继续前进。 I just don't understand why it works fine up until this point but it doesn't in this.我只是不明白为什么它在这一点上工作正常,但它没有。 I asked someone for help and they told me the code runs fine for them, I checked the chrome driver version and it matches the browser version.我向某人寻求帮助,他们告诉我代码对他们来说运行良好,我检查了 chrome 驱动程序版本,它与浏览器版本匹配。

Can someone tell me what is wrong?有人可以告诉我有什么问题吗?

A NullPointerException always implies that the null pointer has been dereferenced , either by evaluating a period after a null reference, either by indexing a null as an array. NullPointerException始终意味着 null 指针已被取消引用,方法是评估 null 引用之后的一段时间,或者将 null 索引为数组。

Let's study this case in your source line:让我们在您的源代码行中研究这个案例:

WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); 

There are just two occurrences of the derefence operator (the period): We must discard the one after By , because it is a class and cannot be null.解引用运算符(句点)只有两次出现:我们必须丢弃By之后的那个,因为它是 class 并且不能是 null。 So we are left with just one culprit: shadow_root3 is surely null.所以我们只剩下一个罪魁祸首: shadow_root3肯定是 null。

Next step: Why shadow_root3 is null?下一步:为什么shadow_root3是null? It is initialized in this previous line:它在前一行中初始化:

root3 = shadow_root2.findElement(By.name("appContent"));
WebElement shadow_root3 = expand_shadow_element(root3);

Method expand_shadow_element returns the shadowRoot of the inputted argument (or null if it has none), so we must conclude that the DOM element named appContent has no shadowRoot set.方法expand_shadow_element返回输入参数的shadowRoot (如果没有,则返回 null),因此我们必须得出结论,名为appContent的 DOM 元素没有设置shadowRoot

From here on, you must debug your HTML page to diagnose why it has no shadowRoot , and how to set it properly.从这里开始,您必须调试您的 HTML 页面来诊断它没有shadowRoot的原因,以及如何正确设置它。

Its not able to find element due to which it throws the null pointer exception.它无法找到引发 null 指针异常的元素。 You may need to change the xpath or by the locator, you are identifying the element.您可能需要更改 xpath 或通过定位器,您正在识别元素。

Below is the code to access the email text box下面是访问 email 文本框的代码

String cssSelectorForHost1 = "my-app[title='FoodPlan']";
String cssSelectorForHost2 = "page-login";
String cssSelectorForHost3 = "mwc-textfield[type='email']";
Thread.sleep(1000);
WebElement shadowDomHostElement0 = driver.findElement(By.cssSelector("my-app[title='FoodPlan']"));
WebElement last0 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement0);
Thread.sleep(1000);
WebElement shadowDomHostElement1 = last0.findElement(By.cssSelector("page-login"));
WebElement last1 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement1);
Thread.sleep(1000);
WebElement shadowDomHostElement2 = last1.findElement(By.cssSelector("mwc-textfield[type='email']"));
WebElement last2 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement2);
Thread.sleep(1000);
last2.findElement(By.cssSelector(".mdc-text-field__input"))

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

相关问题 休眠测试-线程“ main”中的异常java.lang.NullPointerException - Hibernate Test - Exception in thread “main” java.lang.NullPointerException , 线程“main”中的异常 java.lang.NullPointerException - ,Exception in thread "main" java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException” 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“main”中的异常 java.lang.NullPointerException 5 - Exception in thread "main" java.lang.NullPointerException 5 添加到HashMapList时,“主线程中的异常” java.lang.NullPointerException - “Exception in thread ”main" java.lang.NullPointerException when adding to a HashMapList 使用Selenium时收到“线程“ main” java.lang.NullPointerException中的异常” - Receiving “Exception in thread ”main“ java.lang.NullPointerException” when using Selenium 尝试更新文件时,线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException when trying to update file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM