3
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.google.com')
content = driver.find_element_by_css_selector('title')
print content

I can't seem to get any content by any means(tag_name, class_name, name...) What is the problem with this code?

6
  • Are you getting an error? Commented Feb 11, 2016 at 14:02
  • Just this:<selenium.webdriver.remote.webelement.WebElement (session="4aff1f03-26fc-4ddb-9181-534d24ecd0d8", element="{5c5b4c91-740d-40d3-a888-c05a43a1d1e0}")> Commented Feb 11, 2016 at 14:05
  • Then it is working fine. You want to read the text then you should use print content.text With the WebElement you can do any of the actions mentioned [here] (selenium-python.readthedocs.org/…) Commented Feb 11, 2016 at 14:08
  • Yes but now it returns nothing for the title Commented Feb 11, 2016 at 14:44
  • Can you try printing content.tag_name? Commented Feb 11, 2016 at 15:07

2 Answers 2

2

There doesn't seem to be any problem with the code. The is element being identified correctly by your code.

content = driver.find_element_by_css_selector('title')
print content.tag_name

This should confirm that you have indeed located a web element with title tag. The content.text does not return anything since the title element is not part of the page itself. (Similar trouble discussed here in Java)

If you're trying to print the title of the page, use the following driver field

print(driver.title)
Sign up to request clarification or add additional context in comments.

Comments

0

Your output is correct because you are trying to print out the literal web element, not the text contained within it. It is not an error, you're just not printing out the correct thing.

Change your last line to this:

print content.text

2 Comments

content.text does not return the title of the page.
Why not just use driver.title if its the page title you're after? No need to go looking in the DOM.

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.