#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/11 16:36 # @File : unittest_test9_5.py ''' 调用javascript ''' from selenium.webdriver.common.action_chains import ActionChains from selenium import webdriver import unittest import time class ExecuteJavaScriptTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(20) self.driver.maximize_window() self.driver.get('https://www.cnblogs.com/') def test_execute_javascript(self): program_lan = self.driver.find_element_by_xpath('//li[@id="cate_item_2"]/a') program_py = self.driver.find_element_by_xpath('//li/a[@href="/cate/python/"]') self.highlightElement(program_lan) #鼠标先移动到“编程语言”上,然后点击Python ActionChains(self.driver).move_to_element(program_lan).click(program_py).perform() time.sleep(2) def tearDown(self): self.driver.quit() #给元素加上红色边框,2秒后还原 def highlightElement(self,element): self.driver.execute_script("arguments[0].setAttribute('style',arguments[1]);",element, "border:2px solid red;") time.sleep(2) self.driver.execute_script("arguments[0].setAttribute('style',arguments[1]);", element, "") if __name__ == "__main__": unittest.main(verbosity=2)