【问题标题】:Why Am I Getting a NoSuchElementException When Running Tests in Cucumber为什么在 Cucumber 中运行测试时出现 NoSuchElementException
【发布时间】:2019-05-12 22:05:35
【问题描述】:

过去几天我一直在 Cucumber 中编写测试。到目前为止我编写的测试工作正常,我已经能够单击对象,发送键。没问题。

我现在已经找到了这些无论我使用什么选择器都无法找到的页面元素。我尝试过使用等待,但即使它们在页面上清晰可见,也无法访问它们。

这发生在我想要单击的表格行元素和我想要发送键的文本输入中。我在下面包含了后者。

 <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">

这是我目前使用的代码。

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

编辑: 我把代码改成了这个。它更接近于我开始使用的内容

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

这是我现在遇到的错误

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

我切换了我正在做的事情来单击一个打开模式的按钮,并允许我在其中使用一个文本字段。但是按钮没有被看到。

【问题讨论】:

  • EMPLOYEE_IDS_label不存在,应该是EMPLOYEE_label ??
  • 不,不是这样,发帖前我改了一些名字。
  • 您收到什么错误信息? (您应该始终在任何问题中包含完整的错误消息)。您是否检查过定位器是否返回了正确的元素?该页面上是否有多个匹配的元素?尝试在开发控制台中使用$$("input[name='EMPLOYEE_label']")...它返回 1 吗?
  • 另外,wait.until() 会返回等待的元素,因此您可以这样做,WebElement empIDTextInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By .name("EMPLOYEE_label"))); 并且不必为同一个元素再次抓取页面。
  • org.openqa.selenium.TimeoutException:预期条件失败:等待 By.name 定位的元素的可见性:EMPLOYEE_label(尝试 30 秒,间隔 500 毫秒)

标签: java selenium xpath css-selectors cucumber


【解决方案1】:

此错误消息...

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

...暗示 no such element 是使用带有 classname 属性的 Locator Strategy 作为 input.triggerButton 找到的。。 p>

无论在发布问题中的相关 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">

由于元素是动态元素,您必须诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一解决方案:

  • cssSelector:

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

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

【讨论】:

  • 所以我以前试过这个。我添加了以下 2 行代码。 WebDriverWait timeSheetElement = new WebDriverWait(webdriver,100); timeSheetElement.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='tabs-1']/div/table/tbody/tr[1]/td[3]"))).sendKeys("贝内特");但是,当我这样做时,它会引发 NullPointerException。 NPE 可以归因于正在使用的 webdriver,但是如果我尝试在方法中初始化 webdriver,它只会打开一个新的驱动程序,与正在执行的操作无关,所以仍然没有任何作用。
  • 我编辑了代码以反映您在此处编写的内容。我已将 webDriver 传递给杀死 NullPointer 但预期条件每次都失败的方法。 public void userEntersWinUnderTimesheet(WebDriver webdriver) { WebDriverWait timeSheetElement = new WebDriverWait(webdriver,100); timeSheetElement.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='EMPLOYEE_label'][onchange*='EMPLOYEE_IDS']"))).sendKeys(Keywords.ASSOC_LAST_NAME);
猜你喜欢
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 2016-03-26
  • 2020-05-07
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多