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

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐