10

I'm trying to get "Home" text from

<li class="active"><a href="#">Home</a></li>

I tried

element = self.browser.find_element_by_class_name('active')
print element.find_element_by_tag_name('a').text

but it returns empty string. What am I missing here?

1 Answer 1

11

Do it using CSS - .find_element_by_css_selector("li.active a"), or using xpath .find_element_by_xpath("//li[@class='active']/a")

So this is how it would like finally

element = self.browser.find_element_by_css_selector("li.active a")
print element.text
Sign up to request clarification or add additional context in comments.

2 Comments

Ok I finally found the real reason of my problem. I'm using twitter.github.io/bootstrap framework and when browser window is small it hides menu, and that's why I got blank text. Adding maximize_window() solved my problem. @amey thanks for your answer still very helpful as I'm a selenium beginner.
I had a similar problem to OP, I was trying to reference links that were part of like a sub-menu, and were therefore not visible on the screen. When I preformed the necessary actions to make them visible, I was able to read the text.

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.