0

我正在尝试使用 selenium 访问 css 元素的文本。

WebElement test = driver.findElement(By.cssSelector("div.CLFSBTextWithLink"));
System.out.println(test.getText());

这就是我现在正在做的,但输出是空的。

例如,一个名为“Company”的网站上可能有一个链接或文本,当我使用 Selenium 检查元素的位置时,它说目标是 css=div.CLFSBTextWithLink。现在我希望能够以某种方式获得“公司”的价值。

4

1 回答 1

2

If you go to http://viewer.opencalais.com/ and type something like "There was a man named Bob Bob" and hit submit, then you see under the entity person the name Bob Bob. I'm trying to get that name "Bob Bob" basically.


From the getText() docs:

Get the visible (i.e. not hidden by CSS) innerText of this element

But the text is initially hidden. You must first expand the Person element to make it visible. In this particular "Bob Bob" case, you can use:

driver.findElement(By.id("toggleVisibilityImage1")).click();

But always make sure you're clicking the right expander. To make it more general, this selects the expander next to the "Person" category:

//input[@class='collapseExpandIcon' and (../text()='Person')]

and explanation:

SELECT AN <input> ELEMENT
//input[                                                    ]
        THAT IS AN EXPANDER
        @class='collapseExpandIcon'
                                    AND ITS PARENT NODE HAS TEXT "Person"
                                    and (../text()='Person')
于 2013-06-21T15:38:16.250 回答