自动化两个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
browser = webdriver.Chrome()
browser.get("http://www.baidu.com/")
browser.implicitly_wait(10)
print("当前源码:{}".format(browser.page_source))
print("当前标题:{}".format(browser.title))
print("当前链接地址:{}".format(browser.current_url))
print("当前窗口句柄:{}".format(browser.current_window_handle))
print("当前窗口所以句柄:{}".format(browser.window_handles))
with open("baidu.html",'w',encoding='utf=8') as f:
f.write(browser.page_source)
browser.implicitly_wait(10)

browser.find_element(By.LINK_TEXT,'地图').click()
browser.switch_to.window(browser.window_handles[1])
print("新窗口句柄{}".format(browser.current_window_handle))
print(browser.page_source)
# browser.find_element(By.XPATH,'//*[@id="_w_v_l_s_n_s_show_title_1093657"]').send_keys("电子商务")
# browser.implicitly_wait(10)
# browser.find_element(By.XPATH,'//*[@id="au3a"]/div/input[2]').click()
# browser.find_element(By.LINK_TEXT,"电子商务学院").click()
#
# WebDriverWait(browser,20,0.5).until(lambda e1:browser.find_element(By.ID,'kw'))
# browser.find_element(By.ID,'kw').send_keys('手机')
sleep(3)

browser.quit()



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import uuid
driver = webdriver.Chrome()
url = "https://www.baidu.com/"
driver.get(url)
driver.maximize_window()
driver.find_element(By.NAME,"wd").send_keys("bilibili")
driver.find_element(By.ID,"su").click()
windows = driver.window_handles
driver.switch_to.window(windows[-1])
sleep(3)
driver.find_element(By.PARTIAL_LINK_TEXT,"哔哩哔哩 (゜-゜)つロ 干杯~-").click()
windows = driver.window_handles
driver.switch_to.window(windows[-1])
sleep(3)
driver.find_element(By.CLASS_NAME,"nav-search-input").send_keys("python")
driver.find_element(By.CLASS_NAME,"nav-search-btn").click()
sleep(3)
windows = driver.window_handles
driver.switch_to.window(windows[-1])
print(driver.find_element(By.PARTIAL_LINK_TEXT,"python"))
driver.find_element(By.PARTIAL_LINK_TEXT,"黑马程序员python教程").click()

sleep(3)
windows = driver.window_handles
driver.switch_to.window(windows[-1])
random_filename = str(uuid.uuid4())
src="d:/img/{}.png".format(random_filename)
driver.get_screenshot_as_file(src)
driver.quit()