爬虫之selenium和webdriver—基础(五):切换页面和使用代理

一、切换页面

有时候窗口中有许多子tab页面。这时候肯定是需要进行切换的。selenium提供了一个叫做switch_to.window来进行切换,具体切换到哪个页面,可以从driver.window_handles中找到。

 1 from selenium import webdriver
 2 
 3 driver_path = 'D:chromedriverchromedriver.exe'
 4 driver = webdriver.Chrome(executable_path=driver_path)
 5 driver.get('https://www.baidu.com')
 6 
 7 #打开一个新的页面
 8 driver.execute_script("window.open('https://www.douban.com/')")
 9 #driver.current_url返回当前程序在哪个url页面
10 print(driver.current_url)
11 #切换到这个新的页面当中.
12 #driver.window_handles返回一个列表,里面是所有被打开的url页面
13 driver.switch_to.window(driver.window_handles[1])
14 
15 print(driver.current_url)

二、设置代理IP

1 from selenium import webdriver
2 
3 driver_path = 'D:chromedriverchromedriver.exe'
4 
5 options = webdriver.ChromeOptions()
6 options.add_argument("--proxy-server=http://119.119.248.195:9000")
7 driver = webdriver.Chrome(executable_path=driver_path,options=options)
8 driver.get("http://www.httpbin.org/ip")
原文地址:https://www.cnblogs.com/GouQ/p/13094356.html