selenium之菜单栏 层级定位
系统:windows 10,和windows7没有什么不同
语言:python3.7
环境:pycharm+firefox+selenium
功能:多级菜单栏 层级定位 元素定位
这是selenium的首次尝试。程序通过selenium实现了网站多层级菜单栏的元素定位。功能比较简单,备注就没怎么写了。有什么问题可以留言。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019/2/25 18:04
# @Author : 没有蛀牙lm
# @File : test_002.py
# @Software: PyCharm
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
#from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
class Test002(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30) #智能等待
self.base_url = "www.baidu.com"
self.verificationErrors = []
self.accept_next_alert = True
def test_002(self):
driver = self.driver
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("LU")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("123456")
driver.find_element_by_id("loginBtn").click()
web_name = driver.find_element_by_link_text(u"设置")
ActionChains(driver).move_to_element(web_name).perform()
driver.find_element_by_link_text(u"搜索设置")
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == '__main__':
unittest.main()