简体   繁体   English

在黄瓜中运行测试时,为什么会收到NoSuchElementException

[英]Why Am I Getting a NoSuchElementException When Running Tests in Cucumber

I've spent the last few days writing tests in Cucumber. 最近几天,我在Cucumber中编写测试。 The tests I've written so far work fine, I've been able to click objects, sendkeys over. 到目前为止,我编写的测试工作正常,可以单击对象,发送键了。 No problem. 没问题。

I've now gotten to these page elements that cannot be found no matter what selector I use. 现在,无论使用哪种选择器,我都无法找到这些页面元素。 I've tried using wait, but even though they are clearly visible on the page, they can't be accessed. 我尝试使用等待,但是即使它们在页面上清晰可见,也无法访问。

This is happening in both table row elements that I want to click on, and a text input I want to sendkeys to. 我要单击的表行元素和我要向其发送键的文本输入都在发生这种情况。 I've included the latter below. 我在下面包括了后者。

 <input type="text" name="EMPLOYEE_label" value="" class="" 
 onkeypress="return dtPk(event,this);" onkeydown="return dtKd(event,this);" 
 onchange="dltCh(this,'EMPLOYEE__test');" size="30" wbvalid="true" 
isresolving="false">

Here is the code I am using at present. 这是我目前正在使用的代码。

webdriver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(webdriver, 30);  
wait.until(ExpectedConditions.visibilityOfElementLocated(By
    .name("EMPLOYEE_label")));

JOptionPane.showMessageDialog(null, "WebDriver =" + webdriver);
WebElement empIDTextInput  = webdriver.findElement(By.name("EMPLOYEE_label"));
empIDTextInput.sendKeys("Bennett");
Thread.sleep(1000);
gtaProxyPage.clickFindButton().click();
Thread.sleep(1000);

gtaProxyPage.checkAssociateBox().click();
gtaProxyPage.loadTimesheet().click();
Thread.sleep(2000);

EDIT: I changed the code to this. 编辑:我将代码更改为此。 It more closely resembles what I started with 它更像我开始时的样子

     Thread.sleep(30000); 
     //this calls for the input element by className.
    gtaProxyPage.UserEntersNumberUnderTimesheet().click();
    gtaProxyPage.clickFindButton().click();
    gtaProxyPage.checkAssociateBox().click();
    gtaProxyPage.loadTimesheet().click();

This is the error I'm getting now 这是我现在得到的错误

 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"input.triggerButton"}

I switched what I"m doing to click a button that opens a modal, and allows me to use a text field within that. But the button is not being seen. 我切换了操作方式,单击了一个可以打开模式的按钮,并允许我在其中使用文本字段。但是没有看到该按钮。

This error message... 此错误消息...

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"input.triggerButton"}

...implies that the no such element was found using the Locator Strategy with classname attribute as input.triggerButton . ...意味着没有这样的元素用的是发现定位战略类名属性为input.triggerButton。

Irespective of all the changes and manipulation done while publishing the relevant HTML within the question, to send a character sequence to the element: 在问题中发布相关HTML时,不管将所有字符更改和操作如何发送给元素,均需发送字符序列

<input type="text" name="EMPLOYEE_label" value="" class="" onkeypress="return dtPk(event,this);" onkeydown="return dtKd(event,this);" onchange="dltCh(this,'EMPLOYEE__test');" size="30" wbvalid="true" isresolving="false">

As the element is a dynamic element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions: 由于元素是动态元素,因此必须诱使WebDriverWait 使其可单击,并且可以使用以下解决方案之一:

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='EMPLOYEE_label'][onchange*='EMPLOYEE__test']"))).sendKeys("Bennett"); 
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='EMPLOYEE_label' and contains(@onchange, 'EMPLOYEE__test')]"))).sendKeys("Bennett"); 

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

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