无法找到具有类名称,xpath或CSS选择器的元素

时间:2019-01-02 17:03:46

标签: java selenium

到目前为止,我确实有一个非常简单的测试用例,并且我正在尝试测试故障通知。但是我找不到要检查文本的元素。尝试使用xpath,类名和CSS选择器失败。 这是HTML的一部分:

<div class="alert alert-danger fade in">
  <button data-dismiss="alert" class="close">×</button>
  <i class="fa-fw fa fa-times"></i>
  Bitte füllen Sie das Pflichtfeld <b>Name der Veranstaltung</b> korrekt aus.
  <br>Bitte füllen Sie das Pflichtfeld <b>Veranstaltungsdatum / -beginn</b> korrekt aus.
  <br>Bitte füllen Sie das Pflichtfeld <b>Veranstaltungsende</b> korrekt aus.
  <br>Bitte füllen Sie das Pflichtfeld <b>Projektstatus</b> korrekt aus.
  <br>
</div>

这是我的属性的外观:

alert.klasse = "alert alert-danger fade in";
alert.xpath = "/html/body/div[2]/div/div[2]/form/div/div[4]";
alert.cssSelector = ".alert";

它们都不起作用,并且该元素没有要搜索的ID。

堆栈跟踪:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div[2]/div/div[2]/form/div/div[4]

Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:58:41'
System info: host: 'PC2139', ip: '192.168.125.76', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 7664, moz:profile: C:\Users\Sanchez\AppData\Lo..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.1, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 1601fc80-f5fa-4c87-8eec-0b2d5a87ddf5
*** Element info: {Using=xpath, value=/html/body/div[2]/div/div[2]/form/div/div[4]/i}
    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.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.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    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 Elements.vfCP_element_alert.findElement(vfCP_element_alert.java:14)
    at test.veranstaltung.vfCP_VAanlegenBundle.VA_anlegen(vfCP_VAanlegenBundle.java:65)
    at Testcase_VAPflichtfelder.main(Testcase_VAPflichtfelder.java:35)

1 个答案:

答案 0 :(得分:0)

能否请您尝试以下定位器:

CSS Selector: div[class='alert alert-danger fade in']
CSS Selector: .alert.alert-danger.fade.in
XPATH: //button[@class='close']/parent::div
XPATH: //button[@class='close']/../div[@class='alert alert-danger fade in']