简体   繁体   中英

Can't get "print" output in colsole with selenium + python

The problem is, I can't see my print in console, although I should!

def test_123(app):
 wd = app.wd
 app.open_page("page_adress")
 time.sleep(3)
 element = wd.find_element_by_xpath("locator").text
 print(element)

String from my app file:

wd = webdriver.Chrome()

My test runs successful. And one more thing! If after my print command I'm putting some string which leads my test to the crash, I can see print with all other crash information.

Is there a space in the actual print call in your code or is that a typo? I know in python 3.X with Selenium I've only ever used print as follows:

print(element)

Notice there is no space between the print command and the parenthesis.

Updating

Ok so now that I've got my head on right (sorry for the idiot first response), let's take a look at the problem. First things first, when using Selenium and python, I always create a driver object to pass my handle around like so:

driver = webdriver.Chrome()

Secondly, if you happen to know the ID of the element, you can use driver.find_element_by_id('locator') . It's a bit more explicit in my opinion if you care to use it. The big thing though is the first point where you create a driver object. Try giving that a shot and let me know if it's still not working. :-)

The problem is, I can't see my print in console, although I should!

If you use behave as the launcher for the Selenium tests , you can solve this by passing --no-capture as an argument in your behave command:

behave -i  my_feature.feature --no-capture

Without this argument, outputs from print() are captured and discarded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM