admin管理员组

文章数量:1619290

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdrivermon.by import By  # BY:定位策略

Options = webdriver.ChromeOptions()  # 创建谷歌浏览器设置对象
Options.add_experimental_option('detach', True)

# 创建谷歌浏览器对象
Chrome_Object = webdriver.Chrome(service=Service(executable_path='.\chromedriver.exe'), options=Options)
URL = 'https://101.qq/#/hero'
# 得到的数据全保存在Chrome_Object对象中,后续的操作基本上都在这个对象上
Chrome_Object.get(url=URL)
# 有些时候会拿不到数据的原因是selenium判定网页加载完成是等标签页前的刷新图标是否消失
# 为了解决页面没加载完selenium就继续向下执行代码,需要添加等待
# 方式三种:休眠,隐形等待,显示等待
time.sleep(3)
Chrome_Object.maximize_window()
# Chrome_Object.set_window_size(800, 800) 设置窗口大小

# print(Chrome_Object.page_source) 第一种打印数据的方式,但不推荐使用,打印出来是字符串,可以用bs4模块操作

#
# Chrome_Object.get(url='http://taobao')
# time.sleep(2)
# Chrome_Object.back()
# time.sleep(2)
# Chrome_Object.forward()

hero_all_information_list = Chrome_Object.find_elements(
    By.CSS_SELECTOR, '#app > div > div.app-main > div > div.app-main-container.infomation-overview > ul > li')
# print(hero_all_information_list)

for i in hero_all_information_list:
    hero_name = i.find_element(By.CSS_SELECTOR, 'li > div > p').text
    hero_img_href = i.find_element(By.CSS_SELECTOR, 'li > div > div > img').get_attribute('src')
    print(hero_name, hero_img_href)

# Chrome_Object.close()  # 若网页有多个页面只关闭当前页面
# Chrome_Object.quit()  # 关闭所有页面

# nth-of-type:找某个标签的第几个
# nth-child:找同级标签的第几个

本文标签: seleniumlol