Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from selenium import webdriver
  3.  
  4.  
  5. class Facebook:
  6.  
  7.     # metodo que prepara o teste
  8.     def setUp(self):
  9.         self.driver = webdriver.Firefox()
  10.         self.driver.implicitly_wait(30)
  11.         self.base_url = "https://www.facebook.com/"
  12.  
  13.     # metodo que seleciona o form pelo id
  14.     def get_form_by_id(self, driver, id_form):
  15.         form = driver.find_element_by_id(id_form)
  16.         # all_children_by_css = form.find_elements_by_id("email")
  17.         # all_children_by_xpath = form.find_elements_by_xpath(".//*")
  18.         # print(len(all_children_by_xpath))
  19.         # for webElement in all_children_by_xpath:
  20.         # print(webElement.tag_name, webElement.get_attribute("id"), webElement.get_attribute("class"))
  21.         return form
  22.  
  23.     # metodo responsavel por realizar o teste
  24.     def test_facebook(self):
  25.         driver = self.driver
  26.         driver.get(self.base_url + "/")
  27.         form = self.get_form_by_id(driver, "login_form")
  28.         all_children_by_xpath = form.find_elements_by_xpath(".//*")
  29.  
  30.         # codigo que procura os nós do tipo input que possuem a substring "login" como valor de algum de seus atributos
  31.         for childWebElement in all_children_by_xpath:
  32.             if(childWebElement.tag_name == "input"): # procuramos todos nós do tipo input (descendentes do form)
  33.                 childWebElement_attributes_dict = driver.execute_script( # pegamos todos atributos e valores do input e criamos um dicionario
  34.                     'var attr = {}; ' #dicionario do JS que guardará os atributos
  35.                     'for (index = 0; index < arguments[0].attributes.length; ++index) { ' #fazemos um loop pelo NamedNodeMap
  36.             #arguments[0] retorna o primero parametro (argumento) passado para a função. Nesse caso trata-se do childWebElement
  37.             #arguments[0].attributes trata-se de um "dicionario" (NamedNodeMap no JS) dos atributos
  38.                         'attr[arguments[0].attributes[index].name] = arguments[0].attributes[index].value ' #preenchemos o dicionario attr {name:value}
  39.                     '}; return attr;', childWebElement)
  40.                 for chave, valor in childWebElement_attributes_dict.items(): # iteramos por todas posições do dicionario no Python (chave::valor)
  41.                     string_value = str(valor)
  42.                     if 'login' in string_value: # consultamos se o valor da dupla chave::valor da atual posição contem a substring 'login'
  43.                         print(childWebElement_attributes_dict) # mostramos na tela os atributos dos inputs que contem a subtring
  44.                         print()
  45.  
  46.  
  47. fbTest = Facebook()
  48. fbTest.setUp()
  49. fbTest.test_facebook()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement