How to loop python code, unittest, Selenium?

Asked

Viewed 388 times

0

Following the code, how to proceed in a loop to always repeat the same fill N times? Or even picking up the names from a list. txt and populating in "name"?

def teste(self):
    driver = self.driver
    driver.get("http://painelvix.com/galaxia4/formulario_testes.php?r=UUVJcFdZMXlZcE5XdVU2cGVEUjVMYllMNnVBYS9GajVVUkZ3em9tRDZObz0,")
    driver.find_element_by_name("nome").click()
    driver.find_element_by_name("nome").clear()
    driver.find_element_by_name("nome").send_keys("Teste de preenchimento")
    driver.find_element_by_name("email").click()
    driver.find_element_by_name("email").clear()
    driver.find_element_by_name("email").send_keys("[email protected]")
    driver.find_element_by_name("perfil[]").click()
    driver.find_element_by_xpath("//*/text()[normalize-space(.)='Solicitar']/parent::*").click()
    self.assertEqual("Existe um teste pra este email cadastrado em: 28/03/2019", self.close_alert_and_get_its_text())
    driver.close()

Plus the unittest:

if __name__ == "__main__":
unittest.main()

Link to the file . py

File link.

1 answer

1


Well, I think your code has a lot of information unrelated to your problem. So I decided to make a minimal replicable example. Run this code on your computer, see how it works and apply to your program:

from selenium import webdriver


lista_nomes = ['Joao Arruda Botelho','Camila Moraes Sarmento','Regina Moreira Salles']
lista_emails = ['[email protected]', '[email protected]', '[email protected]']

driver = webdriver.Chrome()

for k in range(3):
    driver.get("http://painelvix.com/galaxia4/formulario_testes.php?r=UUVJcFdZMXlZcE5XdVU2cGVEUjVMYllMNnVBYS9GajVVUkZ3em9tRDZObz0,")
    driver.find_element_by_name("nome").click()
    driver.find_element_by_name("nome").clear()
    driver.find_element_by_name("nome").send_keys(lista_nomes[k])
    driver.find_element_by_name("email").click()
    driver.find_element_by_name("email").clear()
    driver.find_element_by_name("email").send_keys(lista_emails[k])
    driver.find_element_by_name("perfil[]").click()

driver.close()
  • Thanks for the tip. But when you run the code, it doesn’t work, it opens only the url, but without any action. driver.find_element_by_name("name".format(k)). send_keys(k) .

  • Sorry, I forgot to add the {}. I’ll edit

  • @Hudsonsouza, try again, please. If it doesn’t work, try to make a replicable example, because then it’s easier to understand what you’re trying to do.

  • I put the link to the file. Basically it’s just filling a small form, with this, loop to fill N times with different names. Briefly, even more: fills sends, fills sends... :)

  • @Hudsonsouza With its full code, it became much easier. See my new solution

  • Thank you, much succinct the code. Show!

  • I think you can put the driver = webdriver.Chrome() out of the loop, after all already started, no need to re-install every time

  • @Guilhermenascimento does this, generates an error after the first fill. Run on your PC to see

  • @Hartnäcking is because it is using driver.close without need also, it would suffice for it out of the loop also ... and of course independent from adjusting the code or not a Sleep can help notice the details, of course this will depend on what the AP will do with the code.

  • @Guilhermenascimento is true. I will edit the code to include these improvements

  • @Hartnäcking also has a deal to do, but this the AP did not even mention, which is about the Alerts that cause Exception, but only if he formulates a new question that you should answer ;) Have a good weekend

  • PS: I think it would be something +or- like the AP code: https://gist.github.com/brcontainer/89c286c237c0c710c0483ebface9c208 (for those curious about the abbreviations used on the site: https://pt.meta.stackoverflow.com/q/6311/3635)

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.