Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python and how to do getText() from an element/seclector

  • 21-09-2015 03:59PM
    #1
    Registered Users, Registered Users 2 Posts: 262 ✭✭


    I’m looking to take a value from a text field to do some arithmetic on

    hour_value = str(browser.find_element("logviewer_hour_text_field", 30))
    log.logger.info("Hour now {0}".str(hour_value.text))


    My output from the element is “None” when I run it. Basically am looking for the equivalent of textField.getText() in Java.

    Do you have any simple examples of getting text from 1 element/selector.


Comments

  • Registered Users, Registered Users 2 Posts: 3,740 ✭✭✭johnmcdnl


    Assuming this is using Selenium WebDriver?
    I'm not overly familar with Python but I don't think you should be wrapping the element with the str() as that will convert the element object to a string which is probably unreadable.

    hour_value = str(browser.find_element("logviewer_hour_text_field", 30))
    log.logger.info(hour_value )

    Have a look at what that prints out - it's probably a long string that means nothing to you. (This is assuming that str() behaves similaring to the .toString() methods in Java.)



    What I think you may need is something along the lines of:

    hour_value = browser.find_element("logviewer_hour_text_field", 30)
    log.logger.info(hour_value.text)


    Once again I don't think you should need to wrap it with str() as .text should return a String in the first place.

    Apologies if this is no use to you - as I said I'm not really that familiar with Python.


  • Registered Users, Registered Users 2 Posts: 1,929 ✭✭✭PrzemoF


    What's behind that "browser' thing?

    >>> import browser
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named browser

    What returns (type and value) browser.find_element? If it's None you won't be able to squeze anything out of it.


  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    As per johnmcdnl's post, I'm assuming you're using selenium webdriver.
    I think John has you on the right track but for more info - see the selenium api docs http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.remote.webelement


Advertisement