首页 > 编程语言 > 详细

python3.6+selenium_调用JavaScript

时间:2019-02-28 15:48:10      阅读:163      评论:0      收藏:0      [点我收藏+]
#!/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)

 

python3.6+selenium_调用JavaScript

原文:https://www.cnblogs.com/xiuxiu123456/p/10450692.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!