做富集分析的网站,做视频网站新手教学,中国工商注册网查询登记,您身边的网站建设专家selenium调用js方法 有时候我们需要控制页面滚动条上的滚动条#xff0c;但滚动条并非页面上的元素#xff0c;这个时候就需要借助js是来进行操作。
一般用到操作滚动条的会两个场景#xff1a;
要操作的页面元素不在当前页面范围#xff0c;无法进行操作#xff0c;需要… selenium调用js方法 有时候我们需要控制页面滚动条上的滚动条但滚动条并非页面上的元素这个时候就需要借助js是来进行操作。
一般用到操作滚动条的会两个场景
要操作的页面元素不在当前页面范围无法进行操作需要拖动滚动条注册时的法律条文需要阅读判断用户是否阅读的标准是滚动条是否拉到最下方
调用js的方法
execute_script(script, *args)滚动条回到顶部
jsdocument.getElementById(id).scrollTop0
driver.execute_script(js)
滚动条拉到底部
jsdocument.documentElement.scrollTop10000
driver.execute_script(js)可以修改scrollTop 的值来定位右侧滚动条的位置0是最上面10000是最底部 以上方法在Firefox和IE浏览器上上是可以的但是用Chrome浏览器发现不管用。Chrome浏览器解决办法
js document.body.scrollTop0
driver.execute_script(js)横向滚动条
js window.scrollTo(100,400)
driver.execute_script(js)代码
from selenium.webdriver.chrome.service
import Service
from selenium import webdriver
from time import sleep
from lxml import etreedef test_scroll():# 创建驱动s Service(./chromedriver.exe)# 创建浏览器driver webdriver.Chrome(services)# 访问页面
driver.get(https://search.jd.com/Search?
keyword%E6%89%8B%E6%9C%BAencutf8suggest1.def.0.SAK7|MIXTAG_SAK7R,SAK7_M_A
M_L5385,SAK7_M_COL_R,SAK7_S_AM_R,SAK7_SC_PD_
R,SAK7_SM_PB_R,SAK7_SS_PM_R,tsabtest_base64_
U2VhcmNobGlzdF80MzkyfGJhc2U_tsabtest|wqsho
ujipvid24340a2def0e4e0cb510af07aa32c89d)# 拉动滚动条到底部jsdocument.documentElement.scrollTop100000driver.execute_script(js)sleep(1)# 创建一个etree对象用于解析数据e etree.HTML(driver.page_source)# 获取数据价格prices e.xpath(//ul[classgl-warpclearfix]/li/div/div/strong/i/text())print(prices)print(len(prices))# 关闭浏览器sleep(3)driver.quit()if __name__ __main__:test_scroll()selenium 等待元素
网速慢AJAX请求数据调试
强制等待 使用 time.sleep 作用当代码运行到强制等待这一行的时候无论出于什么原因都强制等待指定的时间需要通过time模块实现
优点简单
缺点无法做有效的判断会浪费时间
隐式等待 chrome.implicitly_wait(time_num) 到了一定的时间发现元素还没有加载则继续等待我们指定的时间如果超过了我们指定的时间还没有加载就会抛出异常如果没有需要等待的时候就已经加载完毕就会立即执行 优点 设置一次即可
缺点必须等待加载完成才能到后续的操作或者等待超时才能进入后续的操作
from selenium import webdriver
url https://www.baidu.com/
driver webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(10)
print(driver.find_element_by_class_name(next))
print(driver.page_source)显示等待 from selenium.webdriver.support.wait import WebDriverWait 指定一个等待条件并且指定一个最长等待时间会在这个时间内进行判断是否满足等待条件如果成立就会立即返回如果不成立就会一直等待直到等待你指定的最长等待时间如果还是不满足就会抛出异常如果满足了就会正常返回 优点专门用于对指定一个元素等待加载完即可运行后续代码
缺点多个元素都需要要单独设置等待
url https://www.guazi.com/nj/buy/
driver webdriver.Chrome()
driver.get(url)
wait WebDriverWait(driver,10,0.5)
wait.until(EC.presence_of_element_located((By
.CLASS_NAME, next)))
print(driver.page_source)selenium 参数使用 chrome59版本以后可以变成无头的浏览器加以下参数 def test_headless():# 设置参数将浏览器隐藏起来(无头浏览器)options ChromeOptions()options.add_argument(--headless)# 设置驱动
service Service(./chromedriver)# 启动Chrome浏览器driver Chrome(serviceservice,optionsoptions)# 访问页面driver.get(https://www.baidu.com)# 打印代码print(driver.page_source)# 关闭浏览器driver.quit() 代理模式 def test_proxy1():# 设置参数给浏览器设置代理options ChromeOptions()# options.add_argument(--proxyserverhttp://ip:port)options.add_argument(--proxyserverhttp://221.199.36.122:35414)# 设置驱动service Service(./chromedriver)# 启动Chrome浏览器driver Chrome(serviceservice,optionsoptions)# 访问页面 134.195.101.16,driver.get(http://httpbin.org/get)# 打印代码print(driver.page_source)# 关闭浏览器driver.quit()def test_proxy2():from selenium.webdriver.common.proxy
import ProxyType,Proxy# 设置参数给浏览器设置代理ip http://113.76.133.238:35680proxy Proxy()proxy.proxy_type ProxyType.MANUALproxy.http_proxy ipproxy.ssl_proxy ip# 关联浏览器capabilities DesiredCapabilities.CHROMEproxy.add_to_capabilities(capabilities)# 设置驱动service Service(./chromedriver)# 启动Chrome浏览器driver Chrome(serviceservice,desired_capabilitiescapabilities)# 访问页面 134.195.101.16,driver.get(http://httpbin.org/get)# 打印代码print(driver.page_source)# 关闭浏览器driver.quit() 防检测设置 from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptionsoptions ChromeOptions()
options.add_experimental_option(excludeSwitches, [enable-automation])
options.add_experimental_option(useAutomati
onExtension, False)chrome Chrome(chrome_optionsoptions)chrome.execute_cdp_cmd(Page.addScriptToEval
uateOnNewDocument, {source: Object.defineProperty(navigator,
webdriver, {get: () false})
})chrome.get(http://httpbin.org/get)
info chrome.page_sourceprint(info)
sleep(20)使用 window.navigator.webdriver 检测 Selenium实战案例
from selenium.webdriver.chrome.service
import Service
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import Byfrom lxml import etreedef spider_huya():# 创建一个驱动service Service(./chromedriver.exe)# 创建一个浏览器driver Chrome(serviceservice)# 设置隐式等待driver.implicitly_wait(5)# 访问网址driver.get(https://www.huya.com/g/lol)count 1while True:# print(获取了第%d页 % count)# count 1# 提取数据e etree.HTML(driver.page_source)names e.xpath(//i[classnick]/title)person_nums e.xpath(//i[classjs-num]/text())# 打印数据# for n,p in zip(names,person_nums):# print(f主播名:{n} 人气:{p})# 找到下一页的按钮# try:# next_btn driver.find_element(By.XPATH,//a[classlaypage_next])# next_btn.click()# except Exception as e:# breakif
driver.page_source.find(laypage_next) -1:breaknext_btn driver.find_element(By.XPATH,//a[classlaypage_next])next_btn.click()# 关闭浏览器driver.quit()if __name__ __main__:spider_huya()