微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python3.6+selenium_调用JavaScript

#!/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)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐