0

I am trying to use selenium to access the text of a css element.

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

This is what I'm doing right now but the output is empty.

For example the there might be a link or text on a website called "Company" and when I use Selenium to check where the element is, it says that the target is css=div.CLFSBTextWithLink. Now I want to be able to get the value "Company" somehow.

4
  • 2
    What output do you expect? It's not very clear what you're trying to achieve here. Commented Jun 21, 2013 at 15:11
  • For example the there might be a link or text on a website called "Company" and when I use Selenium to check where the element is, it says that the target is css=div.CLFSBTextWithLink. Now I want to be able to get the value "Company" somehow. Commented Jun 21, 2013 at 15:14
  • This should absolutely work. Make sure there is only a single element on the page that is matched by "div.CLFSBTextWithLink". If there more of them, you must either change your selector, or loop over them all. If you could show us a relevant part of the HTMl around your element, we could possibly suggest a correct selection strategy. Commented Jun 21, 2013 at 15:19
  • If you go to 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. Commented Jun 21, 2013 at 15:24

1 Answer 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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.