3

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.

Ser Jah
  • 55
  • 2
  • 6

2 Answers2

1

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.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Luis Moita
  • 47
  • 1
  • 8
  • What `behave` command are you talking about? This doesn't seem relevant to the question asked. – C. Peck May 20 '21 at 21:49
  • behave is a comand used with the Cucumber tool, this command is used to run .features scenarios in python + selenium environments. But adding the --no-option in the command line should work the same way. – Luis Moita May 22 '21 at 01:38
0

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. :-)

Michael Platt
  • 1,297
  • 12
  • 25
  • Oh, unfortunately, with space and without it I have the same result ( – Ser Jah Aug 18 '16 at 15:46
  • Updated the answer above. See if that works for you. – Michael Platt Aug 18 '16 at 15:51
  • Ok so with those changes it still isn't working for you? Also, try using by_id instead of xpath: `element = wd.find_element_by_id("locator").text` – Michael Platt Aug 18 '16 at 16:31
  • I have a filling, that my print is going somewhere but not in the console. Because it appears there only with a crash information, when a made my test to crash – Ser Jah Aug 18 '16 at 16:35
  • Assuming you set up everything correctly and are running the selenium script through command line you should be seeing your output in the same command line. Have you tried debugging the script to see specifically what line of code you are getting the error on? Also, can you post the error you are getting in your command line? – Michael Platt Aug 18 '16 at 16:38
  • I've tried many variants of a different locators. I'm pretty sure it has nothing to do with locator. UPD: I've got no errors in command line. Test passed successful. – Ser Jah Aug 18 '16 at 16:41
  • The element "locator" for sure has text in it correct? Only thing I can think of now would be that there is no actual text in that particular element and that's why you aren't seeing the print. – Michael Platt Aug 18 '16 at 16:49
  • Ok, this is a real locator: element = wd.find_element_by_xpath("//nav/span[.='Практика']").text And that's what I see if a test will crash after my print http://prntscr.com/c7dqjt – Ser Jah Aug 18 '16 at 17:04
  • I think the problem is in pytest framework. For some reason he's not putting my prints in console – Ser Jah Aug 18 '16 at 17:14
  • Looking at the line of code you just posted you don't actually have anything in the square brackets []. Usually you would see something like `[@id='SomeID']`. Have you tried that yet? Here is an example of an xpath I'm using that works successfully: `driver.find_element_by_xpath("//div[@id='task_list']/h3").click()` Notice how I set whatever I'm searching for in xpath (in this case the @id) to be the value you want to find. – Michael Platt Aug 18 '16 at 17:21
  • I've found the solution! http://stackoverflow.com/questions/24617397/how-to-print-to-console-in-py-test – Ser Jah Aug 18 '16 at 17:23
  • Awesome!! Glad you found the answer you were looking for. Best of luck with your application!! – Michael Platt Aug 18 '16 at 17:25
  • Thank you for having responded! – Ser Jah Aug 18 '16 at 17:26