from selenium import webdriver
import time
import traceback
def brower():
# 加载谷歌浏览器驱动
path = "/Users/juanmao/PycharmProjects/chromedriver.exe"
driver = webdriver.Chrome(path)
# 打开百度搜索网址首页
driver.get('http://www.baidu.com')
print(driver.title)
# 登录
# driver.find_element_by_link_text("登录").click()
# driver.find_element_by_id('TANGRAM__PSP_11__userName').send_keys("18233586506")
# driver.find_element_by_id('TANGRAM__PSP_11__password').send_keys("840849182Ll")
# driver.find_element_by_id("TANGRAM__PSP_11__submit").click()
# time.sleep(2)
# TODO 验证码
# 输入搜索词 进入搜索结果页面
driver.find_element_by_id('kw').send_keys('selenium')
driver.find_element_by_id('su').click()
driver.find_element_by_id('kw').clear()
# 获取搜索输入框,修改搜索词,进行搜索
bookNames = []
bookNames.append("修仙小农民")
# bookNames.append("星际武装")
# bookNames.append("孤星斩月")
for bookName in bookNames:
searchBookName(driver,bookName)
# 搜索完毕,退出浏览器
time.sleep(3)
driver.quit()
def searchBookName(driver,bookName):
try:
# 输入搜索词 进入搜索结果页面
driver.find_element_by_id('kw').send_keys(bookName)
driver.find_element_by_id('su').click()
time.sleep(2)
# xpath路径结构和正常浏览器页面略有不同
webelement_a = driver.find_element_by_xpath("//div[@id='wrapper']/div[@id='wrapper_wrapper']/div[@id='container']/div[@id='content_left']/div[@id='1']/*/h3/a")
title = webelement_a.text
print(title)
except Exception as err:
traceback.print_exc()
if __name__ == '__main__':
brower()
备注:Mac环境下,chromedriver后缀确实也为".exe",从压缩包解压后没有后缀,手动改为".exe"的,亲测可用。以下链接上传的资源为后缀修改后的资源,直接使用。
chrome下载链接:https://download.csdn.net/download/qq_37508131/85047496
|