gpt4 book ai didi

java - org.openqa.selenium.NoSuchElementException

转载 作者:太空宇宙 更新时间:2023-11-04 06:55:04 28 4
gpt4 key购买 nike

我正在尝试使用 xsl 表导入登录凭据,例如:用户名密码,使用下面的代码,但在运行时出现 NoSuchElementException 异常,

我的 xsl 表看起来像:

  username    |  password--------------------------jan30selenium | selenium

am using:

  • eclipse
  • webdriver.iostream
  • selenium tool

Login.java:

package Iostream;

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class Login {

public WebDriver driver;

@Test
public void f()throws Exception {
FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
Workbook w= Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.findElement(By.id("f_id")).sendKeys(s.getCell(0,1).getContents());
driver.findElement(By.id("f_pwd")).sendKeys(s.getCell(1,0).getContents());
Thread.sleep(5000);
driver.findElement(By.linkText("input.signin")).click();
Thread.sleep(3000);
}

@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://www.gmail.com");
}

@AfterTest
public void afterTest() {
}
}

异常(exception)是:

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\User\AppData\Local\Temp\scoped_dir5352_17443}, rotatable=false, locationContextEnabled=true, version=33.0.1750.154, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
at org.openqa.selenium.By$ById.findElement(By.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at Iostream.Login.f(Login.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

问题出在哪里?

最佳答案

您遇到这个问题是因为当您 get("http://gmail.com/") 时,id 的("f_id""f_pwd" 不存在。

如果您的目的是登录,那么您可能应该更具体地说明您的 URL,并且应该使用以下代码:

  @Test
public void f()throws Exception {
FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
Workbook w= Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.findElement(By.id("Email")).sendKeys(s.getCell(0,1).getContents());
driver.findElement(By.id("Passwd")).sendKeys(s.getCell(1,0).getContents());
Thread.sleep(5000);
driver.findElement(By.cssSelector("input#signIn")).click();
Thread.sleep(3000);
}
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://accounts.google.com/ServiceLogin?service=mail");
}

另外,您会注意到,当单击登录按钮时,您正在寻找带有文本 "input.signin" 的链接,我认为您想要做的是一个带有 signin 类的 CSS 选择器 input 但该元素也不存在。

关于java - org.openqa.selenium.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com