此文示例用的是Python语言
在Python中selenium执行js很容易,它的执行函数就存在于WebDriver中,Java需要导入其他包。
简单示例
from selenium import webdriver
import random
import pytest
import time
class Test_restart(object):
@pytest.fixture(scope="function", autouse=True)
def setUp(self):
global dr
dr = webdriver.Chrome('D:\\chromedriver456.exe')
yield
dr.quit()
def test_one(self):
# dr = self.dr
#assert random.randint(1, 3) == 3
dr.maximize_window()
dr.get('https://www.baidu.com/')
dr.implicitly_wait(3)
el_text = dr.find_element_by_id('kw')
el_text.send_keys('雪落下的声音')
time.sleep(1)
el_click = dr.find_element_by_id('su')
el_click.click()
time.sleep(3)
#JS语句
js