随笔 - 31  文章 - 0  评论 - 0  阅读 - 2444

<3> selenium

复制代码
复制代码
  1 """selenium"""
  2 
  3 # 驱动浏览器
  4 from selenium import webdriver
  5 # 配置
  6 from selenium. webdriver import ChromeOptions
  7 # 选择器
  8 from selenium. webdriver.common.by import By
  9 # 按键
 10 from selenium.webdriver.common. keys import Keys
 11 # 等待页面
 12 from selenium.webdriver.support.wait import WebDriverWait
 13 # 等待指定标签加载完毕
 14 from selenium.webdriver.support import expected_conditions as EC
 15 # 动作交互
 16 from selenium.webdriver import ActionChains
 17 
 18 
 19 # 1----按下回车按钮
 20 send_keys(Keys.ENTER)
 21 
 22 # 2----元素查找定位
 23 find_element(By.ID, "id")
 24 find_element(By.NAME,"name")
 25 find_element(By.XPATH,"xpath语法")
 26 find_elemnt(By.TAG_NAME, "input")
 27 find_element(By.CLASS_NAME, "classname")
 28 find_element(By.css_SELECTOR,"#id")
 29 find_element(By.LlNK_TEXT, "text")
 30 
 31 # 3----提取内容
 32 # 获取文本 element.text
 33 # 获取属性值 element.get_attribute('属性名')
 34 
 35 # 4----元素交互
 36 text_input.clear() # 清空原来输入的文本内容
 37 
 38 # 5----iframe切换
 39 switch_to.iframe()
 40 
 41 # 6----动作执行
 42 actions = ActionChains(browser)
 43 actions.drag_and_drop(A,B) # A移动到B
 44 actions.perform() # 执行动作链
 45 
 46 # 7----执行JS
 47   # 页面滚动
 48   滚动页面方法execute_script() 该方法可调用原生JavaScript的api
 49   滚动到底部: window.scrollTo(0,document. body.scrollHeight)
 50   滚动到顶部: window. scrollTo(0,0)
 51   # 窗口切换
 52   switch_to.window(browser.window_handles[0])
 53 
 54 # 8----无头模式
 55 from selenium import webdriver
 56 from selenium.webdriver import ChromeOptions
 57 options = ChromeOptions() # 配置文件对象
 58 --------------------------------------------------------------------
 59 options.add_experimental_option('excludeSwitches',[' enable-automation']) # 写入参数
 60 browser = webdriver.Chrome(options=options)
 61 --------------------------------------------------------------------
 62 # 无界面启动,也可以直接设置options.headless=True
 63 options.add_argument("--headless") # 指定无头模式
 64 browser = webdriver.Chrome(options=options)
 65 
 66 
 67 # 9----使用代理IP**
 68 from selenium import webdriver
 69 # 1.创建一个配置对象
 70 options = webdriver.ChromeOptions()
 71 # 2.使用代理
 72 options.add_argument('--proxy-server=http://192.168.129.130')
 73 # 3.创建driver对象
 74 driver = webdriver.Chrome(options=options)
 75 # 4.设置起始的url地址
 76 start_url = 'https://www.baidu.com'
 77 # 访问
 78 driver.get(url=start_url)
 79 
 80 
 81 # 10----替换user-agent
 82 from selenium import webdriver
 83 from fake_useragent import UserAgent
 84 ua = UserAgent()
 85 # 1.创建一个配置对象
 86 options = webdriver.ChromeOptions()
 87 # 2.使用代理
 88 options.add_argument('--user-agent={}'.format(ua.chrome))
 89 # ua = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36'
 90 # options.add_argument('user-agent' + ua)
 91 # 3.创建driver对象
 92 driver = webdriver.Chrome(options=options)
 93 # 4.设置起始的url地址
 94 start_url = 'https://www.baidu.com'
 95 # 访问
 96 driver.get(url=start_url)
 97 print(driver.title)
 98 
 99 # 11----selenium禁止弹窗
100 from selenium import webdriver
101 from selenium.webdriver.chrome.options import Options
102 chrome_options = Options()
103 # 禁止弹窗
104 prefs = {
105   'profile.default_content_setting_values':
106     {
107       'notifications': 2
108     }
109   }
110 # 禁止弹窗加入
111 chrome_options.add_experimental_option('prefs', prefs)
112 driver = webdriver.Chrome(chrome_options=chrome_options)
113 driver.get(url)
114 # 就可以访问无通知弹窗的浏览器了
115 
116 # 12----防检测
117 from selenium import webdriver
118 from selenium.webdriver import ChromeOptions
119 option = ChromeOptions() # 实例化一个ChromeOptions对象
120 option.add_experimental_option('excludeSwitches', ['enable-automation'])# 以键值对的形式加入参数
121 bro = webdriver.Chrome(executable_path='./chromedriver.exe',options=option) # 在调用浏览器驱动时传入option参数就能实现
122 --------------------------------------------------------------------
123 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ })
复制代码

 

复制代码
posted on   不是霉蛋  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示