Selenium (Python & Java) a simple example

Realize the function

Open a browser - Access Baidu Home - Search keyword "selenium" - to open the first five pages of search results and links to respective outputs of Title and URL

Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

driver = webdriver.Firefox()
driver.get("https://www.baidu.com")

print("="*10+"Before search"+"="*10)

#打印当前页面title
title = driver.title;
print(title)

#打印当前页面URL
url = driver.current_url
print(url)

#搜索关键词selenium
input = driver.find_element_by_id("kw")
input.send_keys("selenium")
submitbtn = driver.find_element_by_id("su")
submitbtn.send_keys(Keys.ENTER) # 或submitbtn.click()
sleep(2)

print("="*10+"After search"+"="*10)
print("Click the first 5 links on the result page")

#获取搜索结果,并打开前五个结果的链接(不含广告链接)
# handle = driver.current_window_handle
results = driver.find_elements_by_css_selector("div.result.c-container>h3>a")

for i in range(5):
    results[i].click()
    sleep(2)
    driver.switch_to.window(driver.window_handles[1])
    print("Result %d: "%(i+1)+driver.title+" "+driver.current_url)
    sleep(1)
    ActionChains(driver).send_keys(Keys.CONTROL,1)
    driver.switch_to.window(driver.window_handles[0])

Java

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;

public class FirefoxTest {

	public static void main(String[] args) throws InterruptedException {
		//由于Firefox没有安装在默认路径下,所以要指定Firefox的安装路径,否则无法启动浏览器
		System.setProperty("webdriver.firefox.bin","D:\\Mozilla Firefox\\firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.baidu.com");
		
//		//由于Chrome安装在默认路径下,故可以直接启动
//		WebDriver driverC = new ChromeDriver();
//		driverC.get("https://www.baidu.com");
		
		System.out.println("=========="+"Before search"+"==========");
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		
		driver.findElement(By.id("kw")).sendKeys("selenium");
		driver.findElement(By.id("su")).submit(); //或driver.findElement(By.id("su")).click();
		Thread.sleep(2000);
		
		System.out.println("=========="+"After search"+"==========");
		System.out.println("Click the first 5 links on the result page");
		
//		String handle = driver.getWindowHandle();
		List<WebElement> search_result = driver.findElements(By.cssSelector("div.result.c-container>h3>a"));
		
		for (int i=0;i<5;i++) {
			search_result.get(i).click();
			Thread.sleep(3000);
			String[] handles = new String[driver.getWindowHandles().size()];
			driver.getWindowHandles().toArray(handles);
			driver.switchTo().window(handles[1]);
			System.out.printf("Result %d: "+driver.getTitle()+" "+driver.getCurrentUrl(),i+1);
			System.out.println();
			Thread.sleep(1000);
			driver.switchTo().window(handles[0]);	
		}
	}

}

Export

==========Before search==========
百度一下,你就知道
https://www.baidu.com/
==========After search==========
Click the first 5 links on the result page
Result 1: Selenium - Web Browser Automation https://www.seleniumhq.org/
Result 2: 功能自动化测试工具——Selenium篇 http://www.51testing.com/zhuanti/selenium.html
Result 3: selenium用法详解 - CSDN博客 https://blog.csdn.net/qq_29186489/article/details/78661008
Result 4: selenium中文网 | selenium安装、selenium使用、selenium中文、selenium下载 http://www.selenium.org.cn/
Result 5: 自动化测试工具Selenium入门 - CSDN博客 https://blog.csdn.net/liuzhixiong_521/article/details/78607236

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u013378642/article/details/81626192