selenium定位到元素后获取其属性,Selenium WebDriver从CSS属性“content”获取文本on :: before伪元素...

I am trying to verify the text "The information provided is either invalid or incomplete." is displayed using Selenium/WebDriver in Java.

I have the following HTML:

  • Required: Server Name
  • Required: Receiving Port

with the following CSS code:

#validationError:before {

content: 'The information provided is either invalid or incomplete.';

}

Here's my current java code:

WebElement errorBox = browser.findElement(By.id("validationError"));

Assert.assertNotNull("Could not find validation errors", errorBox);

System.out.println("Error Explanation: " + error.getCssValue("content") + "\n");

List errorList = errorBox.findElements(By.tagName("li"));

for (WebElement error : errorList) {

System.out.println("Error: " + error.getText());

}

System.out.println("\nError Full Text: " + errorBox.getText());

This is my output of the above code:

Error Explanation:

Error: Required: Server Name

Error: Required: Receiving Port

Error Full Text: Required: Server Name

Required: Receiving Port

I can't seem to find a way to verify the text "The information provided is either invalid or incomplete." is displayed. Calling getText() on the web element also does not return the expected string, but will display any other normal text within that div. Any ideas?

解决方案

I am not 100% sure if WebDriver can retrieve pseudo element content for you. I think you would need to use Javascript. Below works, I tested.

String script = "return window.getComputedStyle(document.querySelector('#validationError'),':before').getPropertyValue('content')";

JavascriptExecutor js = (JavascriptExecutor)driver;

String content = (String) js.executeScript(script);

System.out.println(content);

This should print

The information provided is either invalid or incomplete.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值