Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.openqa.selenium.NoSuchElementException:无此类元素:无法定位元素:_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java org.openqa.selenium.NoSuchElementException:无此类元素:无法定位元素:

Java org.openqa.selenium.NoSuchElementException:无此类元素:无法定位元素:,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有一个自动脚本,我试图转到一个页面,检查如果我没有登录,我是否无法下载报告。我的测试步骤是: GIVEN that I am an anonymous user on the WTR WHEN I navigate to the Reports Centre tool AND select a 'Download' button on a report THEN the login or subscribe gate will be displayed AND if I click the '

我有一个自动脚本,我试图转到一个页面,检查如果我没有登录,我是否无法下载报告。我的测试步骤是:

GIVEN that I am an anonymous user on the WTR
WHEN I navigate to the Reports Centre tool
AND select a 'Download' button on a report
THEN the login or subscribe gate will be displayed
AND if I click the 'Subscribe' button
THEN the Subscribe page will be displayed
AND when I navigate back to the Reports Centre tool
AND select a 'Download' button on a report
THEN the login or subscribe gate will be displayed
AND if I click the 'Login' button
THEN the Login page will be displayed
但是,当我运行脚本时,总是会出现以下错误:
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:

这是我的stacktrace:

  (Session info: chrome=81.0.4044.92)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'd038d2fb3cc5', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.76-linuxkit', java.version: '11.0.7'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 81.0.4044.92, chrome: {chromedriverVersion: 81.0.4044.69 (6813546031a4b..., userDataDir: /tmp/.com.google.Chrome.h0SS5v}, goog:chromeOptions: {debuggerAddress: localhost:46475}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: 073a630f009a019a022a5d9a2b2...}
Session ID: 073a630f009a019a022a5d9a2b268f81
*** Element info: {Using=xpath, value=//div[@id='search-ui']/div/div[2]/div[3]/div[2]/div/div/div/div[5]/button/span}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at com.lbr.testcases.gxr.wtr.gating.anon.gating_wtr_reports_centre_anon.reportscentre(gating_wtr_reports_centre_anon.java:33)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) 
我有一个java屏幕截图实用程序,它在结束测试之前执行。起初,我可以看到应用程序没有完全加载,所以我添加了一个显式等待。然后它看起来像一个cookie横幅阻止了点击,所以我在横幅上添加了一个点击接受按钮,但我仍然在我的屏幕截图中看到这个横幅。当我在其他脚本上出现类似错误时,这种方法就起作用了

我看到了:我试过含蓄的等待,但没有成功。我现在重新使用显式等待。我没有尝试过这种方法,因为我没有使用Intelliji。还有其他建议吗?我是Java新手

这是我的剧本:

package com.lbr.testcases.gxr.wtr.gating.anon;

import org.junit.Test;
import org.junit.Before;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import static org.hamcrest.CoreMatchers.is;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import java.util.*;
import com.lbr.utils.Screenshot;
import java.net.MalformedURLException;
import java.net.URL;
public class gating_wtr_reports_centre_anon {
  private WebDriver driver;
  JavascriptExecutor js;

  @Before
  public void setUp() throws MalformedURLException {
    driver = new RemoteWebDriver(new URL("localhost"), DesiredCapabilities.chrome());
    js = (JavascriptExecutor) driver;
    new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    Screenshot.TakeScreenshot(driver);
    driver.quit();
  }
  @Test
  public void gatingwtrreportscentreanon() {
    // GIVEN that I am an anonymous user on the WTR
    // WHEN I navigate to the Reports Centre tool
    driver.get("https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
    new WebDriverWait(driver, (30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']"))); 
    driver.findElement(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']")).click();
    // AND select a 'Download' button on a report
    driver.findElement(By.cssSelector(".report-search-results .lexcomponent-card:nth-child(1) .download-text")).click();
    // THEN the login or subscribe gate will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
    // AND if I click the 'Subscribe' button
    driver.findElement(By.linkText("Subscribe")).click();
    // THEN the Subscribe page will be displayed
    MatcherAssert.assertThat(driver.findElement(By.cssSelector(".text--upper")).getText(), is("WHY SUBSCRIBE?"));
    // AND when I navigate back to the Reports Centre tool 
    driver.get("https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
    new WebDriverWait(driver, (30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']"))); 
    driver.findElement(By.xpath("//button[@class='optanon-allow-all accept-cookies-button']")).click();
    // AND select a 'Download' button on a report
    driver.findElement(By.cssSelector(".report-search-results .lexcomponent-card:nth-child(1) .download-text")).click();
    // THEN the login or subscribe gate will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
    // AND if I click the 'Login' button
    driver.findElement(By.linkText("Login")).click();
    // THEN the Login page will be displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h1[contains(.,\'ACCOUNT LOGIN\')]")).getText(), is("ACCOUNT LOGIN"));
  }
}
package com.lbr.testcases.gxr.wtr.gating.anon;
导入org.junit.Test;
导入org.junit.Before;
导入org.hamcrest.matcherasert;
导入org.junit.After;
导入静态org.hamcrest.CoreMatchers.is;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.remote.RemoteWebDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.openqa.selenium.JavascriptExecutor;
导入java.util.*;
导入com.lbr.utils.Screenshot;
导入java.net.MalformedURLException;
导入java.net.URL;
公共课门控、报告、中心{
私有网络驱动程序;
JavascriptExecutor js;
@以前
public void setUp()引发畸形的DurException{
driver=新的RemoteWebDriver(新的URL(“localhost”),DesiredCapabilities.chrome());
js=(JavascriptExecutor)驱动程序;
新的HashMap();
}
@之后
公共无效拆卸(){
截图。截图(司机);
driver.quit();
}
@试验
公共无效gatingwtrreportscentreanon(){
//假设我是WTR上的匿名用户
//当我导航到报告中心工具时
驱动程序。获取(“https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
新的WebDriverWait(driver,(30)).until(ExpectedConditions.elementtobelickable(By.xpath(//button[@class='optanon-allow-all-accept cookies button']);
findElement(By.xpath(“//button[@class='optanon-allow-all-accept cookies button']))。单击();
//并在报告上选择“下载”按钮
driver.findelelement(By.cssSelector(“.report search results.lexcomponent card:nth child(1).下载文本”)。单击();
//然后将显示登录或订阅门
matcherasert.assertThat(driver.findElement(By.xpath(//h1[contains(,,'ACCOUNT LOGIN\')]))).getText(),is(“ACCOUNT LOGIN”);
//如果我点击“订阅”按钮
driver.findElement(By.linkText(“订阅”))。单击();
//然后将显示订阅页面
matcherasert.assertThat(driver.findelelement(By.cssSelector(“.text--upper”)).getText()是(“为什么订阅?”);
//当我导航回报告中心工具时
驱动程序。获取(“https://www.worldtrademarkreview.com/tools/reports-centre/?page=1&ps=20");
新的WebDriverWait(driver,(30)).until(ExpectedConditions.elementtobelickable(By.xpath(//button[@class='optanon-allow-all-accept cookies button']);
findElement(By.xpath(“//button[@class='optanon-allow-all-accept cookies button']))。单击();
//并在报告上选择“下载”按钮
driver.findelelement(By.cssSelector(“.report search results.lexcomponent card:nth child(1).下载文本”)。单击();
//然后将显示登录或订阅门
matcherasert.assertThat(driver.findElement(By.xpath(//h1[contains(,,'ACCOUNT LOGIN\')]))).getText(),is(“ACCOUNT LOGIN”);
//如果我点击“登录”按钮
driver.findElement(By.linkText(“Login”))。单击();
//然后将显示登录页面
matcherasert.assertThat(driver.findElement(By.xpath(//h1[contains(,,'ACCOUNT LOGIN\')]))).getText(),is(“ACCOUNT LOGIN”);
}
}

请提供url或html源代码。一般来说,尽量避免使用这种“丑陋”的xpath。请提供url或html源代码。一般来说,尽量避免使用这种“丑陋”的xpath。