java我是否使用了正确的代码来获取文本文件中注册页面的输出
我对自动化非常陌生,我正在尝试使用cucumber框架自动化注册页面并在文本文件中获得输出。但我的Then部分似乎没有正确的代码,并且出现以下错误: 组织。openqa。硒。NoTouchElementException:没有这样的元素:无法定位元素 详情如下: 特色:客户注册 场景概要:注册表的验证 如果我能够访问客户登记表 当我尝试填写“”和“” 我点击提交 然后,我应该能够导航到注册成功的客户详细信息页面
Examples:
| customerName | age | address | phoneNumber | email |
| testuserOne | 21 | London | 789456123 |xyz@xyz.com|
| testuserTwo | 23 | Paris | 789456128 |xvyxvy.com |
Step file:
package StepDefintion;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import java.io.*;
public class stepDefintion{
WebDriver driver;
@Given("^I am able to access customer registration form$")
public void i_am_able_to_access_customer_registration_form() throws Throwable
{
System.setProperty("webdriver.chrome.driver", "D:\\learning\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
driver.get("http://apps.e-box.co.in/CustomerRegistration/Index");
}
@When("^I try to fill in \"([^\"]*)\" (\\d+) \"([^\"]*)\" (\\d+) and \"([^\"]*)\"$")
public void i_try_to_fill_in_and(String arg1, int arg2, String arg3, int arg4, String arg5) throws Throwable
{
driver.findElement(By.name("cname")).sendKeys(arg1);
driver.findElement(By.name("age")).sendKeys(String.valueOf(arg2));
driver.findElement(By.name("address")).sendKeys(arg3);
driver.findElement(By.name("phonenumber")).sendKeys(String.valueOf(arg4));
driver.findElement(By.name("email")).sendKeys(arg5);
}
@When("^I click on Submit$")
public void i_click_on_Submit() throws Throwable
{
driver.findElement(By.id("submit")).click();
}
@Then("^I should be able to navigate to Registered Succesfully page with customer details$")
public void i_should_be_able_to_navigate_to_Registered_Succesfully_page_with_customer_details() throws Throwable, IOException
{
String S1 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[normalize-space()='Registered Succesfully']//following::td[2]"))).getAttribute("innerHTML");
File file = new File ("D:\\learning\\CucumberParameterization\\src\\output.txt");
FileWriter FW = new FileWriter (file);
PrintWriter PW = new PrintWriter (FW);
PW.print(S1);
PW.close();
}
}
Error Stack Trace :
org.openqa.selenium.WebDriverException: chrome not reachable
(Session info: chrome=64.0.3282.167)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 4.01 seconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: '****', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73), userDataDir=***\AppData\Local\Temp\scoped_dir8160_2912}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=64.0.3282.167, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=false, acceptInsecureCerts=false, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: cf56a3d609cb4b5dd6b20413ec2fbdab
*** Element info: {Using=xpath, value=//h2[normalize-space()='Registered Succesfully']//following::td[2]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:861)
at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:859)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
at StepDefintion.stepDefintion.i_should_be_able_to_navigate_to_Registered_Succesfully_page_with_customer_details(stepDefintion.java:50)
at ?.Then I should be able to navigate to Registered Succesfully page with customer details(D:/learning/CucumberParameterization/src/featureFiles/Customer.feature:8)
# 1 楼答案
错误说明了一切:
似乎WebDriver实例无法定位WebElement,您可能需要诱导一些WebDriverWait使元素可见,如下所示:
在这里您可以找到关于NoSuchElementException的详细讨论
更新A:
根据您的评论,您现在看到以下错误:
主要原因是您使用的二进制文件不兼容,因为:
因此,ChromeDriver和Chrome Browser之间存在明显的不匹配
解决方案
更新B:
根据您的评论,您看到的错误如下:
您使用的是硒版本(发布日期:2015-07-30 02:56:46)[根据您问题中的错误堆栈跟踪]
因此硒版本2.52.0和铬驱动=2.35之间的时间间隔几乎为3年,并且不兼容。因此,ChromeDriver无法生成新的Chrome浏览器进程
解决方案
# 2 楼答案
您缺少表单标记,其中包含表
您可以使用以下XPATH:-
或
//td[text()='Customer Name']/following-sibling::td/input
# 3 楼答案
其他人都说了。我只想补充一点:当出现这样的错误时,检查元素的xpath是否正确非常有用。我发现这个答案很好地解释了这个问题:How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?
# 4 楼答案
看起来您使用的是绝对Xpath,而webdriver无法找到使用它的元素。您可以尝试使用以下Xpath: