selenium的简单使用:
# coding=utf-8
"""
author:lei
function:
"""
from selenium import webdriver
url = "https://sanhe.58.com/"
options = webdriver.ChromeOptions()
options.binary_location = r"D:\文件2\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(r"D:\文件\软件\chromedriver_win32\chromedriver.exe", options=options)
driver.get(url)
print(driver.current_url) # https://sanhe.58.com/
print(driver.window_handles) # ['CDwindow-7C3441AFCF4816FAD8DD46D6DB9AFFBA']
# 定位并点击租房按钮
el = driver.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/div/div[1]/div[1]/span[1]/a")
el.click()
# 页面未进行切换
print(driver.current_url) # https://sanhe.58.com/
print(driver.window_handles) # ['CDwindow-7C3441AFCF4816FAD8DD46D6DB9AFFBA', 'CDwindow-6D5FDC3A190B651E663EC78AA3A004E9']
# 切换到新打开的页面
driver.switch_to.window(driver.window_handles[-1])
el_list = driver.find_elements_by_xpath("/html/body/div[6]/div[2]/ul/li/div[2]/h2/a[1]")
print(len(el_list))
- 复制链接
- 举报