How to use find_element_by_xpath method in Airtest

Best Python code snippet using Airtest

test_int.py

Source:test_int.py Github

copy

Full Screen

...60 db.drop_all()61 print("--------------------------END-OF-TEST----------------------------------------------\n\n\n-------------------------UNIT-AND-SELENIUM-TESTS----------------------------------------------")62class TestUserForms(TestBase):63 def test_registration(self):64 self.driver.find_element_by_xpath(65 "/html/body/div/form/a").click()66 time.sleep(1)67 self.driver.find_element_by_xpath(68 '/html/body/div/form/input[2]').send_keys('user3')69 self.driver.find_element_by_xpath('/html/body/div/form/input[3]').send_keys(70 'testing@testing.me')71 self.driver.find_element_by_xpath('/html/body/div/form/input[4]').send_keys(72 'password3')73 self.driver.find_element_by_xpath('/html/body/div/form/input[5]').send_keys(74 'password3')75 self.driver.find_element_by_xpath(76 '/html/body/div/form/input[6]').click()77 time.sleep(1)78 print(self.driver.current_url)79 assert url_for('home') in self.driver.current_url80 def test_login(self):81 self.driver.find_element_by_xpath(82 '/html/body/div/form/input[2]').send_keys('user1')83 self.driver.find_element_by_xpath(84 '/html/body/div/form/input[3]').send_keys('password')85 self.driver.find_element_by_xpath(86 '/html/body/div/form/input[4]').click()87 time.sleep(1)88 assert url_for('home') in self.driver.current_url89 def test_update_user(self):90 self.driver.find_element_by_xpath(91 '/html/body/div/form/input[2]').send_keys('user1')92 self.driver.find_element_by_xpath(93 '/html/body/div/form/input[3]').send_keys('password')94 self.driver.find_element_by_xpath(95 '/html/body/div/form/input[4]').click()96 time.sleep(1)97 self.driver.find_element_by_xpath(98 '/html/body/div/nav/ul[2]/li/a').click()99 time.sleep(1)100 user = self.driver.find_element_by_xpath(101 '/html/body/div/div[1]/div/h1').text102 email = self.driver.find_element_by_xpath(103 '/html/body/div/div[1]/div/h3').text104 self.driver.find_element_by_xpath(105 '/html/body/div/nav/ul[2]/li/a').click()106 time.sleep(1)107 self.driver.find_element_by_xpath(108 '/html/body/div/form/input[2]').clear()109 self.driver.find_element_by_xpath(110 '/html/body/div/form/input[2]').send_keys('user_1-updated')111 self.driver.find_element_by_xpath(112 '/html/body/div/form/input[3]').clear()113 self.driver.find_element_by_xpath(114 '/html/body/div/form/input[3]').send_keys('updated@email.test')115 self.driver.find_element_by_xpath(116 '/html/body/div/form/input[4]').click()117 time.sleep(1)118 assert url_for('view_user') in self.driver.current_url119 new_user = self.driver.find_element_by_xpath(120 '/html/body/div/div[1]/div/h1').text121 new_email = self.driver.find_element_by_xpath(122 '/html/body/div/div[1]/div/h3').text123 assert new_user != user124 assert new_email != email125 def test_log_out(self):126 self.driver.find_element_by_xpath(127 '/html/body/div/form/input[2]').send_keys('user1')128 self.driver.find_element_by_xpath(129 '/html/body/div/form/input[3]').send_keys('password')130 self.driver.find_element_by_xpath(131 '/html/body/div/form/input[4]').click()132 time.sleep(1)133 self.driver.find_element_by_xpath(134 '/html/body/div/nav/ul[2]/li/a').click()135 time.sleep(1)136 self.driver.find_element_by_xpath(137 '/html/body/div/div[2]/div/a[2]').click()138 time.sleep(1)139 assert url_for('login') in self.driver.current_url140 def test_delete_user(self):141 self.driver.find_element_by_xpath(142 '/html/body/div/form/input[2]').send_keys('user1')143 self.driver.find_element_by_xpath(144 '/html/body/div/form/input[3]').send_keys('password')145 user = Users.query.filter_by(user_name='user1')146 self.driver.find_element_by_xpath(147 '/html/body/div/form/input[4]').click()148 time.sleep(1)149 self.driver.find_element_by_xpath(150 '/html/body/div/nav/ul[2]/li/a').click()151 time.sleep(1)152 self.driver.find_element_by_xpath(153 '/html/body/div/div[2]/div/a[1]').click()154 time.sleep(1)155 users = Users.query.all()156 assert url_for('login') in self.driver.current_url157 assert user not in users158class TestEventForms(TestBase):159 def test_add_event(self):160 self.driver.find_element_by_xpath(161 '/html/body/div/form/input[2]').send_keys('user1')162 self.driver.find_element_by_xpath(163 '/html/body/div/form/input[3]').send_keys('password')164 self.driver.find_element_by_xpath(165 '/html/body/div/form/input[4]').click()166 time.sleep(1)167 self.driver.find_element_by_xpath(168 '/html/body/div/nav/ul[1]/li/a').click()169 time.sleep(1)170 self.driver.find_element_by_xpath(171 '/html/body/div/form/input[2]').send_keys('tester event')172 self.driver.find_element_by_xpath(173 '/html/body/div/form/input[3]').send_keys('tester event')174 self.driver.find_element_by_xpath(175 '/html/body/div/form/input[4]').send_keys('11/08/2020')176 self.driver.find_element_by_xpath(177 '/html/body/div/form/input[5]').click()178 time.sleep(1)179 assert url_for('home') in self.driver.current_url180 event_text = self.driver.find_element_by_xpath(181 '/html/body/div/table/tbody/tr[1]/td[1]').text182 assert event_text == 'tester event'183 def test_delete_event(self):184 self.driver.find_element_by_xpath(185 '/html/body/div/form/input[2]').send_keys('user1')186 self.driver.find_element_by_xpath(187 '/html/body/div/form/input[3]').send_keys('password')188 self.driver.find_element_by_xpath(189 '/html/body/div/form/input[4]').click()190 time.sleep(1)191 event = Events.query.filter_by(title='Event1').first()192 self.driver.find_element_by_xpath(193 '/html/body/div/table/tbody/tr[1]/td[3]/a').click()194 time.sleep(1)195 self.driver.find_element_by_xpath(196 '/html/body/div/div[2]/div/a[2]').click()197 events = Events.query.all()198 assert url_for('home') in self.driver.current_url199 assert event not in events200 def test_edit_event(self):201 self.driver.find_element_by_xpath(202 '/html/body/div/form/input[2]').send_keys('user1')203 self.driver.find_element_by_xpath(204 '/html/body/div/form/input[3]').send_keys('password')205 self.driver.find_element_by_xpath(206 '/html/body/div/form/input[4]').click()207 time.sleep(1)208 self.driver.find_element_by_xpath(209 '/html/body/div/table/tbody/tr[1]/td[3]/a').click()210 time.sleep(1)211 event_text = self.driver.find_element_by_xpath(212 '/html/body/div/div[1]/div/h1').text213 self.driver.find_element_by_xpath(214 '/html/body/div/nav/ul[2]/li/a').click()215 time.sleep(1)216 self.driver.find_element_by_xpath(217 '/html/body/div/form/input[2]').clear()218 self.driver.find_element_by_xpath(219 '/html/body/div/form/input[2]').send_keys('edit event')220 self.driver.find_element_by_xpath(221 '/html/body/div/form/input[5]').click()222 assert url_for('event_view', id=1) in self.driver.current_url223 edit_event_text = self.driver.find_element_by_xpath(224 '/html/body/div/div[1]/div/h1').text225 assert edit_event_text != event_text226 def test_add_user_event(self):227 self.driver.find_element_by_xpath(228 '/html/body/div/form/input[2]').send_keys('user1')229 self.driver.find_element_by_xpath(230 '/html/body/div/form/input[3]').send_keys('password')231 self.driver.find_element_by_xpath(232 '/html/body/div/form/input[4]').click()233 time.sleep(1)234 self.driver.find_element_by_xpath(235 '/html/body/div/table/tbody/tr[1]/td[3]/a').click()236 time.sleep(1)237 self.driver.find_element_by_xpath(238 '/html/body/div/div[2]/div/a[1]').click()239 time.sleep(1)240 self.driver.find_element_by_xpath(241 '/html/body/div/form/input[2]').send_keys('user2')242 self.driver.find_element_by_xpath(243 '/html/body/div/form/input[3]').click()244 groups = Groups.query.filter_by(event_id=1).all()245 assert len(groups) == 2246 assert url_for('event_view', id=1) in self.driver.current_url247if __name__ == '__main__':...

Full Screen

Full Screen

prueba_mesa.py

Source:prueba_mesa.py Github

copy

Full Screen

...6import random7browser = webdriver.Firefox()8browser.get('http://localhost:8080/')9def create_employees(employees):10 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[3]/a').click()11 time.sleep(2)12 for emp in employees:13 print(emp)14 val = random.randint(1, 2)15 sueldo = random.randint(81, 1500)16 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[1]/div/button').click()17 browser.find_element_by_xpath('//*[@id="nombre"]').clear()18 browser.find_element_by_xpath('//*[@id="nombre"]').send_keys(emp)19 browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[2]/form/select/option['+str(val)+']').click()20 browser.find_element_by_xpath('//*[@id="sueldo"]').clear()21 browser.find_element_by_xpath('//*[@id="sueldo"]').send_keys(str(sueldo))22 browser.find_element_by_xpath('//*[@id="act"]').click()23 browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[3]/button['+str(val)+']').click()24 if val == 2:25 time.sleep(2)26 browser.switch_to_alert().accept()27 print(val)28 time.sleep(1)29def delete_employee(num):30 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[3]/a').click()31 time.sleep(2)32 browser.find_element_by_xpath('//*[@id="'+str(num)+'"]').click()33def add_table(tables):34 35 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[2]/a').click()36 for t in tables:37 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[1]/div/button').click()38 browser.find_element_by_xpath('//*[@id="capacidad"]').clear()39 browser.find_element_by_xpath('//*[@id="capacidad"]').send_keys(str(t))40 browser.find_element_by_xpath('//*[@id="addTableButton"]').click()41 time.sleep(1)42 browser.switch_to_alert().accept()43 time.sleep(3)44def delete_table(num):45 browser.find_element_by_xpath('//*[@id="'+str(num)+'"]').click()46 time.sleep(1)47 browser.switch_to_alert().accept()48def asign_table():49 tables = browser.find_elements_by_name('assignModal')50 browser.find_element_by_xpath('//*[@id="assignTable"]').click()51 time.sleep(1)52 browser.find_element_by_xpath('//*[@id="clientName"]').clear()53 browser.find_element_by_xpath('//*[@id="clientName"]').send_keys('Aaron Zajac')54 browser.find_element_by_xpath('//*[@id="empleadosSelect"]').click()55 browser.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div/div[2]/form/select/option[1]').click()56 browser.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div/div[3]/button[2]').click()57 time.sleep(1)58 browser.switch_to_alert().accept()59 time.sleep(3)60 61def add_product(prods):62 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[5]/a/span').click()63 val = random.randint(90, 150)64 for p in prods:65 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[1]/div/button').click()66 browser.find_element_by_xpath('//*[@id="nombreDelProducto"]').clear()67 browser.find_element_by_xpath('//*[@id="nombreDelProducto"]').send_keys(str(p))68 browser.find_element_by_xpath('//*[@id="costoDelProducto"]').clear()69 browser.find_element_by_xpath('//*[@id="costoDelProducto"]').send_keys(str(val))70 browser.find_element_by_xpath('//*[@id="precioDelProducto"]').clear()71 browser.find_element_by_xpath('//*[@id="precioDelProducto"]').send_keys(str(val+20))72 tipo = browser.find_element_by_name('tipoSelect')73 opt = tipo.find_elements_by_tag_name('option')74 for option in opt:75 if option.get_attribute('value') == "PlatoFuerte":76 option.click()77 browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[3]/button[2]').click()78 time.sleep(1)79 browser.switch_to_alert().accept()80 time.sleep(3)81def add_account():82 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[4]/a').click()83 val = random.randint(1, 3)84 browser.find_element_by_name('orderModal').click()85 time.sleep(1)86 browser.find_element_by_xpath('//*[@id="productSelect"]').click()87 browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[2]/form/select/option['+str(val)+']').click()88 browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[3]/button[2]').click()89 time.sleep(1)90 browser.switch_to_alert().accept()91 92def entrega_orden():93 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[6]/a').click()94 body = browser.find_element_by_tag_name('tbody')95 orders = body.find_elements_by_tag_name('tr')96 print(orders)97 for order in range(0, len(orders)-1):98 print(order)99 time.sleep(2)100 if order == 0:101 browser.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/div/div[1]/div/div/div/table/tbody/tr/td[5]/button[2]').click()102 time.sleep(2)103 browser.switch_to_alert().accept()104 else:105 browser.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/div/div[1]/div/div/div/table/tbody/tr['+str(order)+']/td[5]/button[2]').click()106 time.sleep(2)107 browser.switch_to_alert().accept()108def cancel_ordrer():109 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[6]/a').click()110 browser.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/div/div[1]/div/div/div/table/tbody/tr/td[5]/button[1]').click()111 time.sleep(2)112 browser.switch_to_alert().accept()113def pay_account():114 browser.find_element_by_xpath('/html/body/div/div/nav/ul/li[4]/a').click()115 time.sleep(2)116 browser.find_element_by_name('pay').click()117 time.sleep(2)118 browser.switch_to_alert().accept()119employees = ['Aaron Zajac', 'Emiliano Abascal', 'Semy Levy']120productos = ['Camote', 'Pepino', 'Zanahoria']121tables = [1, 2]122# create_employees(employees)123# delete_employee(6)124# add_table(tables)125# delete_table(22)126# asign_table()127# add_product(productos)128# time.sleep(2)...

Full Screen

Full Screen

create_interface.py

Source:create_interface.py Github

copy

Full Screen

...15 def test_login(self):16 driver = self.driver17 time.sleep(2)18 # driver.find_element_by_link_text(u"登录").click()19 driver.find_element_by_xpath("//*[contains(text(),'登录')]").click()20 time.sleep(2)21 driver.find_element_by_xpath("//input[@type='text']").click()22 driver.find_element_by_xpath("//input[@type='text']").clear()23 driver.find_element_by_xpath("//input[@type='text']").send_keys("test_yoyo")24 driver.find_element_by_xpath("//input[@type='password']").clear()25 driver.find_element_by_xpath("//input[@type='password']").send_keys("123456")26 driver.find_element_by_id("login").click()27 time.sleep(2)28 self.assertEqual(driver.current_url,"http://www.doclever.cn/controller/console/console.html")29 def test_creat_interface(self):30 driver = self.driver31 time.sleep(5)32 #进入MyHome项目33 # driver.find_element_by_xpath("//*[contains(text(),'MyHome')]").click()34 driver.find_element_by_xpath(35 u"(.//*[normalize-space(text()) and normalize-space(.)='MyHome'])[1]/following::div[6]").click()36 #在Homework文件夹下创建接口createport37 driver.find_element_by_xpath("//div[@id='group1']/div[3]/div[3]/i").click()38 driver.find_element_by_xpath("(//input[@type='text'])[4]").click()39 driver.find_element_by_xpath("(//input[@type='text'])[4]").clear()40 driver.find_element_by_xpath("(//input[@type='text'])[4]").send_keys("createport")41 driver.find_element_by_xpath("(//input[@type='text'])[8]").click()42 driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='GET'])[1]/following::span[1]").click()43 driver.find_element_by_xpath("(//input[@type='text'])[9]").click()44 driver.find_element_by_xpath("(//input[@type='text'])[9]").clear()45 driver.find_element_by_xpath("(//input[@type='text'])[9]").send_keys("/user/login")46 driver.find_element_by_xpath("(//input[@type='text'])[15]").click()47 driver.find_element_by_xpath("(//input[@type='text'])[15]").clear()48 driver.find_element_by_xpath("(//input[@type='text'])[15]").send_keys("username")49 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='必选'])[2]/following::span[2]").click()50 driver.find_element_by_xpath("(//input[@type='text'])[44]").click()51 driver.find_element_by_xpath("(//input[@type='text'])[44]").clear()52 driver.find_element_by_xpath("(//input[@type='text'])[44]").send_keys("seist")53 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='新增'])[2]/following::button[1]").click()54 driver.find_element_by_xpath("(//input[@type='text'])[18]").click()55 driver.find_element_by_xpath("(//input[@type='text'])[18]").clear()56 driver.find_element_by_xpath("(//input[@type='text'])[18]").send_keys("password")57 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='必选'])[3]/following::span[2]").click()58 driver.find_element_by_xpath("(//input[@type='text'])[47]").click()59 driver.find_element_by_xpath("(//input[@type='text'])[47]").clear()60 driver.find_element_by_xpath("(//input[@type='text'])[47]").send_keys("123456")61 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='新增'])[2]/following::button[1]").click()62 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='运行'])[1]/following::button[1]").click()63 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='自动保存'])[1]/following::span[1]").click()64 driver.find_element_by_xpath("(//input[@type='text'])[5]").click()65 driver.find_element_by_xpath("(//input[@type='text'])[5]").clear()66 driver.find_element_by_xpath("(//input[@type='text'])[5]").send_keys("www.doclever.cn")67 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='导入分组'])[2]/following::span[1]").click()68 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='取消'])[1]/following::button[1]").click()69 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='运行'])[1]/following::span[2]").click()70 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='自动保存'])[1]/following::span[1]").click()71 driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='生成'])[1]/following::span[1]").click()72 def test_logout(self):73 driver = self.driver74 time.sleep(3)75 logout = driver.find_element_by_xpath('//*[contains(text(),"test_yoyo")]')76 ActionChains(driver).move_to_element(logout).perform() # 把鼠标放到元素上,其他的什么都不动77 time.sleep(1)78 driver.find_element_by_xpath(79 "(.//*[normalize-space(text()) and normalize-space(.)='帮助中心'])[1]/following::li[1]").click()80 time.sleep(5)81 self.assertTrue(self.is_element_present(By.LINK_TEXT, "登录"))82if __name__ == '__main__':83 suite = unittest.TestSuite()84 suite.addTests[CreateInterface("test_login"),CreateInterface("test_creat_interface"),CreateInterface("test_logout")]85 runner = unittest.TextTestRunner...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful