#使用JS操作,如果webdriver操作不了,就用这个
#使用webdriver经常会有定位不到或者操作不了元素的情况,用JS来操作基本就不会有问题
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import unittest
import traceback
import time
class TestDemo(unittest.TestCase):
def setUp(self):
# 启动ie浏览器
self.driver = webdriver.Ie(executable_path = "g:\\IEDriverServer")
def test_executeScript(self):
url = "http://www.sogou.com"
# 访问sogou首页
self.driver.get(url)
# 构造JavaScript查找百度首页的搜索输入框的代码字符串
searchInputBoxJS = "document.getElementById('query').value='selenium自动化';"
# 构造JavaScript查找百度首页的搜索按钮的代码字符串
searchButtonJS = "document.getElementById('stb').click()"
try:
# 通过JavaScript代码在百度首页搜索输入框中输入“光荣之路”
self.driver.execute_script(searchInputBoxJS)
time.sleep(2)
# 通过JavaScript代码点击搜狗首页上的搜索按钮
self.driver.execute_script(searchButtonJS)

本文探讨了在Selenium自动化测试中,当WebDriver遇到无法定位或操作元素的问题时,如何利用JavaScript进行有效操作,以确保测试流程的顺利进行。
最低0.47元/天 解锁文章
10万+

被折叠的 条评论
为什么被折叠?



