Java实现方式

1.添加依赖

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.0.0</version>	
    <scope>test</scope>
</dependency>

2.下载浏览器驱动

下载相应的浏览器驱动,浏览器驱动和浏览器版本需对应 点击下载

3.新建test类,代码如下

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestDemo {
    private WebDriver driver;
    @BeforeClass
    public void setUp() {
        //下载的浏览器驱动要放在对应的谷歌浏览器安装位置下
        System.setProperty("webdriver.chrome.driver","C:\\Users\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    @Test
    public void Seleniumtest1() {
        System.out.println("In test 1");
        driver.get("http://www.baidu.com");
        String expectedPageTitle = "百度一下,你就知道";
        System.out.println(driver.getTitle());
        Assert.assertTrue(driver.getTitle().contains(expectedPageTitle), "Test Failed");
    }
    @AfterClass
    public void tearDown() {
        if(driver!=null)
            driver.quit();
    }
}

python实现方式

1.下载selenium库

2.新建test类,代码如下

import unittest
from time import sleep
from selenium import webdriver
class Demo_API(unittest.TestCase):
    # if chromedriver is not in your path, you’ll need to add it here
    def setUp(self):
        self.driver = webdriver.Chrome(r'C:\Users\MNN\AppData\Local\Google\Chrome\Application\chromedriver.exe')

    def tearDown(self):
        yield self.driver
        self.driver.quit()

    def test_api(self):
        self.driver.get("http://www.baidu.com")
        sleep(2)
if __name__=='__main__':
    unittest.main()
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐