Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Selenium 标签危险,卡在这里_Selenium - Fatal编程技术网

Selenium 标签危险,卡在这里

Selenium 标签危险,卡在这里,selenium,Selenium,我需要验证用户名和密码字段。当密码或用户名不正确时。我想验证显示的错误消息 下面显示了HTML。类显示label-label-danger,但如果按类使用findelement,它将抛出“未找到元素”异常 <div> <div class="form-group"> <label for="password">Password</label> <input id="loginform-password" class="fo

我需要验证用户名和密码字段。当密码或用户名不正确时。我想验证显示的错误消息

下面显示了HTML。类显示label-label-danger,但如果按类使用findelement,它将抛出“未找到元素”异常

<div>
  <div class="form-group">
    <label for="password">Password</label>
    <input id="loginform-password" class="form-control" type="password" value="ssssssssssss" name="LoginForm[password]">
    <div class="clear"></div>
    <div class="label label-danger">Incorrect username or password.</div>
  </div>
  <input id="loginform-type" type="hidden" value="company"name="LoginForm[type]">
  <button class="nd-btn" type="submit">LOGIN</button>
</div>

我的密码是

WebDriver driver = new FirefoxDriver();
// To explicitly wait for 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to Required URL
driver.get("http://ndustrious.com/");
// Maximize the Window
driver.manage().window().maximize();
// To explicitly wait for 5 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// click on EXECEPTIONAL ORGANIZATIONS
driver.findElement(By.cssSelector("a[href*='/site/company-login']")).click();
    driver.findElement(By.id("loginform-username")).sendKeys("abc");
    driver.findElement(By.id("loginform-password")).clear();
    WebElement labelElement = driver.findElement(By.xpath("//div[@class = 'label label-danger']"));
    labelElement.getText();

By.className
不适用于多个类属性,您需要使用xpath:

WebElement labelElement = driver.findElement(By.xpath("//div[@class = 'label label-danger']")); 
第二种方法也不起作用,因为文本不是链接元素的文本,而是
div

如果要打印文本,请执行以下操作:

labelElement.getText();

你能试试这个吗


driver.findElement(By.xpath(“//div[contains(text(),'error')]”;

如果要通过.classname()映射获取WebElement,则只能传递一个类。最好通过

driver.findElement(By.className("label-danger"));

如果要在定位器中使用多个CSS类名,则需要使用CSS选择器

driver.findElement(By.cssSelector("div.label.label-danger")).getText();

请在您调用findelee的地方共享您的代码。@Mattias Lindberg,我使用了driver.findelee(By.className(“label-label-danger”);或driver.findelee(By.linkText(“不正确的用户名或密码”));请将问题标题更新为一个问题,并与您的问题更相关。这会引发一个
NoTouchElementException
Thank u@drkthng这对我有帮助。但当我尝试打印错误消息时,这是我在控制台窗口中得到的
[[FirefoxDriver:firefox on WINDOWS(906394ab-3539-4c3d-aa8b-cc24130cedaa)]->除了getAttribute(“value”)->我甚至更新了我的答案->别忘了投票;-)我已经更新了我的代码,请检查一下并帮助我记住这也可以像我对@drkthng answer提到的那样工作
driver.findElement(By.className("label-danger"));
driver.findElement(By.cssSelector("div.label.label-danger")).getText();