以编程方式查找网页中一段文本的字体属性

Finding font properties of a piece of text in a web page programmatically

我在网页中有一个特定的关键字(可显示,不是属性的一部分),我需要知道它的字体样式、字体颜色和背景颜色,以及其他属性。我正在使用 php,我拥有的三个选项是:-

  1. 查找关键字所在元素的层次结构,并 然后组合内联样式、各种 css 文件等。工作量太大 感觉就像重新发明轮子。
  2. 通过xpath递归查找关键字,然后使用a webdriver 来查找属性。
  3. 使用网络驱动程序使用一些内置函数直接在文本中查找关键字,然后查找属性。

有人可以 suggestions/pros/cons 提供替代方案吗?如果您的选择是 (3),您更愿意使用哪个 webdriver 以及您将使用的功能是什么?

基本上策略应该是,

  1. 使用xpath contains查找文本中所有含有'displayable'的元素
  2. 并访问每个元素的 css 属性

我不知道 php,但在 Java 你可以

List<WebElement> elements = driver.findElements(By.xpath("//*[text()[contains(.,'displayable')]]"));
for(WebElement element : elements) {
  System.out.println(element.getCssValue("font-style"));
}

php 代码应该像下面这样,但我不能保证,因为我不太了解。参考文献here

$elements = $driver->findElements(WebDriverBy::xpath("//*[text()[contains(.,'displayable')]]"));
foreach($elements as $e)
{
    echo $e->getCSSValue('font-style');
}

如果您有兴趣了解此 xpath 的工作原理,请查看一个很棒的答案 here