selenium webdriver webelement获取标签方法

1.等待浏览器加载完指定的标签

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

span_page_info = WebDriverWait(driver, 20, 0.5).until(
                EC.presence_of_element_located((By.CLASS_NAME, "page-info")))

2.得到标签中的内容

  e.g.1/8,希望得到1/8

# 得到标签中间夹的字符串
page_count = span_page_info.get_attribute('innerHTML')

# 得到标签的属性值
class_value = span_page_info.get_attribute('class')

3.通过标签名称(类型)获取标签

img_elem_list = div_discription.find_elements_by_tag_name('img')

4.通过标签class获取标签

# 当只通过一个class定位时
# 
div_shop_hesper = driver.find_element_by_css_selector(".shop-hesper-bd.grid")
       

5. 当网页动态加载时,比如淘宝的细节大图。则滚动浏览器来加载图片

# 控制浏览器向下滚动2000个像素
driver.execute_script("window.scrollBy(0,2000)")

 

你可能感兴趣的:(爬虫)