selenium使用总结

1.网页爬取

from selenium import webdriver
browser = webdriver.Edge()    #python根路径配置驱动文件
browser.get('')
soup = BeautifulSoup(browser.page_source)
soup.prettify()

2.下拉框选择、网页点击、输入框

#下拉框选择
from selenium.webdriver.support.select import Select

sel_element1 = browser.find_element_by_xpath('//*[@id="selectedDataset"]') Select(sel_element1).select_by_visible_text("Daily Summaries")

 select = Select(driver.find_element(By.XPATH,'/html/body/div/main/article/section[1]/div/div[1]/form/div[2]/div[1]/div[2]/div[1]/div/select'))
 # 打印所有的options元素
 all_options = [i.text for i in select.options]


#网页点击
browser.find_element_by_xpath('//*[@id="dateRangeContainer"]/input').click()

#输入框输入
from selenium.webdriver.common.by import By

browser.find_element(By.XPATH,'/html/body/div[1]/div[2]/div/div[1]/div[2]/form/fieldset[4]/div/input').send_keys('shanghai')

#选择元素的另一种方式
driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/div/div[2]/span[2]/ul/li[{}]".format(i)).click()
#执行js代码
driver1.execute_script("return document.body.scrollHeight")

3.执行js代码使某些信息显示--style = ''

js="document.getElementsByClassName('noaa-daterange-dialog')[0].style.display='block'"
browser.execute_script(js)

4.页面滑动加载操作

 last_height = driver1.execute_script("return document.body.scrollHeight")
             
            # 模拟下拉操作,直到滑动到底部
            while True:
                # 模拟下拉操作
                driver1.execute_script("window.scrollTo(0, document.body.scrollHeight);")
             
                # 等待页面加载
                time.sleep(3)
             
                # 获取当前页面的高度
                new_height = driver1.execute_script("return document.body.scrollHeight")
             
                # 判断是否已经到达页面底部
                if new_height == last_height:
                    break
             
                # 继续下拉操作
                last_height = new_height

5.窗口转换

 handles = driver2.window_handles
 driver2.switch_to.window(handles[-1])

 

posted @ 2024-02-28 09:44  呱呱、、  阅读(7)  评论(0编辑  收藏  举报