Selenium 库学习笔记
Posted 河南骏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium 库学习笔记相关的知识,希望对你有一定的参考价值。
#--*-- coding:utf-8 --*-- #selenium基本使用 # from selenium import webdriver # from selenium.webdriver.common.by import By # from selenium.webdriver.support import expected_conditions as EC # from selenium.webdriver.support.wait import WebDriverWait # # browser=webdriver.Chrome() # browser.get('https://www.baidu.com/') # wait=WebDriverWait(browser,10) # input=wait.until(EC.presence_of_element_located((By.ID,'q'))) # button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search'))) # print(input,button)
若出现下面的错误,selenium.common.exceptions.WebDriverException:
Message: 'chromedriver' executable needs to be in PATH.
提示chrome driver没有放置在正确的路径下,于是下载chrome dirver,放在python的scripts里面即可。
# from selenium import webdriver # from selenium.webdriver.common.by import By # from selenium.webdriver.common.keys import Keys # from selenium.webdriver.support import expected_conditions as EC # from selenium.webdriver.support.wait import WebDriverWait # import code # # #创建一个浏览器对象 # browser=webdriver.Chrome() # try: # #开启一个浏览器并访问https://www.baidu.com # browser.get('https://www.baidu.com') # #在打开的网页响应中根据id查找元素 获取到查询框 # input=browser.find_element_by_id('kw') # #想查询框中输入python # input.send_keys('Python') # #code.interact(local=locals()) #代码运行到此暂停 # #模拟回车 # input.send_keys(Keys.ENTER) # #显示等待,等待10秒 # wait=WebDriverWait(browser,10) # #显示等待指定某个条件,然后设置最长等待时间。如果在这个时间还没有找到元素,那么便会跑出异常 # wait.until(EC.presence_of_element_located((By.ID,'content_left'))) # #输出当前的url # print(browser.current_url) # #输出Cookies # print(browser.get_cookies()) # #输出页面响应内容 # print(browser.page_source) # finally: # pass # #关闭浏览器 # browser.close() #查找单个元素 # from selenium import webdriver # #申明一个浏览器对象 # browser=webdriver.Chrome() # #使用浏览器访问淘宝 # browser.get('https://www.taobao.com') # #在响应结果中通过id查找元素 # input_first=browser.find_element_by_id('q') # #在响应结果中通过css选择器查找元素 # input_second=browser.find_element_by_css_selector('#q') # #在响应结果中通过xpath查找元素 # input_third=browser.find_element_by_xpath('//*[@id="q"]') # print(input_first) # print(input_second) # print(input_third) # browser.close() # # find_element_by_name 通过name查找 # find_element_by_xpath 通过xpath查找 # find_element_by_link_text 通过链接查找 # find_element_by_partial_link_text 通过部分链接查找 # find_element_by_tag_name 通过标签名称查找 # find_element_by_class_name 通过类名查找 # find_element_by_css_selector 通过css选择武器查找 # find_element_by_name 通过name查找 # find_element_by_xpath 通过xpath查找 # find_element_by_link_text 通过连接查找 # find_element_by_partial_link_text 通过部分连接查找 # find_element_by_tag_name 通过标签名称查找 # find_element_by_class_name 通过类名查找 # find_element_by_css_selector 通过css选择查找 # from selenium import webdriver # from selenium.webdriver.common.by import By # # browser=webdriver.Chrome() # browser.get('https://www.taobao.com') # #通过使用By.xxx指定查找方式 # input=browser.find_element(By.ID,'q') # print(input) # browser.close() #多个元素 # from selenium import webdriver # from selenium.webdriver.common.by import By # # browser=webdriver.Chrome() # browser.get('https://www.taobao.com') # #根据选择查找多个元素 # input_first=browser.find_elements_by_css_selector('.service-bd li') # input_second=browser.find_elements(By.CSS_SELECTOR,'.service-bd li') # print(input_first) # print(input_second) # browser.close() #元素交互操作 # import time # from selenium import webdriver # from selenium.webdriver.common.by import By # # browser=webdriver.Chrome() # browser.get('https://www.taobao.com') # #根据ID查找元素 # input_search=browser.find_element(By.ID,'q') # #模拟输入PSV到输入框 # input_search.send_keys('PSV') # time.sleep(2) # #清空输入 # input_search.clear() # input_search.send_keys('3DS') # button=browser.find_element(By.CLASS_NAME,'btn-search') # #模拟点击 # button.click() #交互动作 # from selenium import webdriver # from selenium.webdriver import ActionChains # # browser = webdriver.Chrome() # url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' # browser.get(url) # # 切换id为iframeResult的frame # browser.switch_to.frame('iframeResult') # source = browser.find_element_by_css_selector('#draggable') # target = browser.find_element_by_css_selector('#droppable') # actions = ActionChains(browser) # actions.drag_and_drop(source, target) # actions.perform() #执行javascript # from selenium import webdriver # # # 申明一个浏览器对象 # browser = webdriver.Chrome() # browser.get('https://www.zhihu.com/explore') # # 执行JavaScript脚本 # browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') # browser.execute_script('alert("To Bottom")') #获取元素信息 #获取属性 # from selenium import webdriver # from selenium.webdriver.common.by import By # # # 申明一个浏览器对象 # browser = webdriver.Chrome() # browser.get('https://www.zhihu.com/explore') # logo = browser.find_element(By.ID,'zh-top-link-logo') # # 获取属性 # print(logo.get_attribute('class')) # print(logo) # browser.close() #获取文本值 # from selenium import webdriver # from selenium.webdriver.common.by import By # # # 申明一个浏览器对象 # browser = webdriver.Chrome() # browser.get('https://www.zhihu.com/explore') # submit = browser.find_element(By.ID,'zu-top-add-question') # # 获取文本值 # print(submit.text) # print(submit) # browser.close() #获取ID,位置,标签名,大小 # from selenium import webdriver # from selenium.webdriver.common.by import By # # # 申明一个浏览器对象 # browser = webdriver.Chrome() # browser.get('https://www.zhihu.com/explore') # submit = browser.find_element(By.ID,'zu-top-add-question') # # 获取id 0.04584255991652042-1 # print(submit.id) # # 获取位置 'y': 7, 'x': 675 # print(submit.location) # # 获取标签名称 button # print(submit.tag_name) # # 获取大小 'width': 66, 'height': 32 # print(submit.size) # browser.close() #frame # from selenium import webdriver # from selenium.common.exceptions import NoSuchElementException # # browser = webdriver.Chrome() # url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' # browser.get(url) # # 将操作的响应数据换成iframeResult # browser.switch_to.frame('iframeResult') # source = browser.find_element_by_css_selector('#draggable') # print(source) # try: # logo = browser.find_element_by_class_name('logo') # except NoSuchElementException: # print('NO LOGO') # # 切换成父元素 # browser.switch_to.parent_frame() # logo = browser.find_element_by_class_name('logo') # print(logo) # print(logo.text) #等待 #隐式等待 # from selenium import webdriver # # browser = webdriver.Chrome() # # 等待10秒 # browser.implicitly_wait(10) # browser.get('https://www.zhihu.com/explore') # input = browser.find_element_by_class_name('zu-top-add-question') # print(input) #显示等待 #显式等待指定某个条件,然后设置最长等待时间。 # 如果在这个时间还没有找到元素,那么便会抛出异常了。 # from selenium import webdriver # from selenium.webdriver.common.by import By #from selenium.webdriver.support import expected_conditions as EC # from selenium.webdriver.support.wait import WebDriverWait # browser = webdriver.Chrome() # browser.get('https://www.taobao.com/') # # 显示等待10s # wait = WebDriverWait(browser, 10) # # 等待直到元素加载出 # input = wait.until(EC.presence_of_element_located((By.ID, 'q'))) # # 等待直到元素可点击 # button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search'))) # print(input, button) #title_is 标题是某内容 # title_contains 标题包含某内容 # presence_of_element_located 元素加载出,传入定位元组,如(By.ID, 'p') # visibility_of_element_located 元素可见,传入定位元组 # visibility_of 可见,传入元素对象 # presence_of_all_elements_located 所有元素加载出 # text_to_be_present_in_element 某个元素文本包含某文字 # text_to_be_present_in_element_value 某个元素值包含某文字 # frame_to_be_available_and_switch_to_it frame加载并切换 # invisibility_of_element_located 元素不可见 # element_to_be_clickable 元素可点击 # staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新 # element_to_be_selected 元素可选择,传元素对象 # element_located_to_be_selected 元素可选择,传入定位元组 # element_selection_state_to_be 传入元素对象以及状态,相等返回True,否则返回False # element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返回False # alert_is_present 是否出现Alert #前进后退 # import time # from selenium import webdriver # # browser = webdriver.Chrome() # browser.get('https://www.baidu.com/') # browser.get('https://www.taobao.com/') # browser.get('https://www.python.org/') # browser.back() # time.sleep(1) # browser.forward() # browser.close() #Cookies # from selenium import webdriver # # browser = webdriver.Chrome() # browser.get('https://www.zhihu.com/explore') # # 获得cookies # print(browser.get_cookies()) # # 添加cookie # browser.add_cookie('name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey') # print(browser.get_cookies()) # # 删除所有cookies # browser.delete_all_cookies() # print(browser.get_cookies()) #选项卡管理 # import time # from selenium import webdriver # # browser = webdriver.Chrome() # browser.get('https://www.baidu.com') # # 打开一个选项卡 # browser.execute_script('window.open()') # print(browser.window_handles) # # 选择第二个选项卡 # browser.switch_to_window(browser.window_handles[1]) # browser.get('https://www.taobao.com') # time.sleep(1) # browser.switch_to_window(browser.window_handles[0]) # browser.get('https://python.org') #异常处理 # from selenium import webdriver # from selenium.common.exceptions import TimeoutException, NoSuchElementException # # browser = webdriver.Chrome() # try: # browser.get('https://www.baidu.com') # except TimeoutException: # print('Time Out') # try: # browser.find_element_by_id('hello') # except NoSuchElementException: # print('No Element') # finally: # browser.close()
以上是关于Selenium 库学习笔记的主要内容,如果未能解决你的问题,请参考以下文章