Selenium教程__使用execute_script执行JavaScript(11)

selenium的包含的方法已能完全满足UI自动化,但是有些时候又不得不用到执行JS的情况,比如在一个富文本框中输入1W个字,使用send_keys方法将经历漫长的输入过程,如果换成使用JS的innerHTML方法就能够很快的完成输入。

selenium执行JavaScript代码的方法有两种:

  1. 执行原生的JS代码
  2. 执行需格式化的JS代码

先简单写个html界面帮助演示




    
    
    Document


    
    
    















































div标签

一、执行原生JS代码

这种方法是将所有操作全部交给js代码去实现,包括定位元素以及对元素的操作,因为原生JS定位元素有一定的局限性所以不是所有的元素都能使用该方法。

实现代码如下

from selenium import webdriver
import time

# 创建Chrome实例对象
driver = webdriver.Chrome()
# 打开浏览器并访问百度
driver.get("file:///C:/Users/41850/Desktop/test.html")
# 窗口最大化
driver.maximize_window()
time.sleep(2)

long_str = "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" \
             "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" \
             "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" \
             "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" \
             "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容"

# 方法一:执行原生的JS代码
# 例1:向富文本插值
js = f"document.getElementById('kw').innerHTML='{long_str}';"
driver.execute_script(js)
time.sleep(1)

# 例2:向input框插值
js = f'document.getElementById("kw1").value="selenium"'
driver.execute_script(js)
time.sleep(1)

# 例3:滚动浏览器界面至指定元素
js = f"document.getElementById('div').scrollIntoView()"
driver.execute_script(js)
time.sleep(1)

# 例4:点击按钮
js = f'document.getElementById("btn").click()'
driver.execute_script(js)


# 关闭浏览器
# driver.quit()

二、先定位元素,再执行JS代码

第二种方法是先使用selenium定位元素,然后使用execute_script方法将传入的参数格式化成JS语句再执行,与第一种方法相比,更易用。

实现代码如下

from selenium import webdriver
import time

# 创建Chrome实例对象
driver = webdriver.Chrome()
# 打开浏览器并访问百度
driver.get("file:///C:/Users/41850/Desktop/test.html")
# 窗口最大化
driver.maximize_window()
time.sleep(2)

long_str = "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" \
             "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" \
             "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" \
             "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" \
             "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" \
             "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" \
             "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容"

# 方法二:执行需格式化的JS代码
# 例1:向富文本插值
el = driver.find_element_by_id("kw")
js = f"arguments[0].innerHTML='{long_str}';"
driver.execute_script(js, el)
time.sleep(1)

# 例2:向input框插值
el = driver.find_element_by_id("kw1")
js = f'arguments[0].value="selenium"'
driver.execute_script(js, el)
time.sleep(1)

# 例3:滚动浏览器界面至指定元素
el = driver.find_element_by_id("div")

js = f"arguments[0].scrollIntoView()"
driver.execute_script(js, el)
time.sleep(1)

# 例4:点击按钮
el = driver.find_element_by_id("btn")
js = f'arguments[0].click()'
driver.execute_script(js, el)


# 关闭浏览器
# driver.quit()

事必有法,然后有成- 最后祝大家早日达到测试的天花板!


Selenium教程__使用execute_script执行JavaScript(11)_第1张图片


以下是我收集到的比较好的学习教程资源,虽然不是什么很值钱的东西,如果你刚好需要,可以留言【777】直接拿走就好了

你可能感兴趣的:(软件测试,自动化测试,javascript,selenium,软件测试,自动化测试,自动化测试框架)