WebDriver常见操作
本文当个记录贴,记录WebDriver常用的一些函数(含自己封装的函数)
让WebDriver使用浏览器用户设置
1 option = webdriver.ChromeOptions() 2 option.add_argument(r'user-data-dir=C:\Users\AlphaINF\AppData\Local\Google\Chrome\User Data') 3 driver = webdriver.Chrome(options = option)
封装好的setAttribute
1 def setAttribute(driver, elementObj, attributeName, value): 2 # 封装设置页面对象的属性值的方法 3 # 调用JavaScript代码修改页面元素的属性值,arguments[0]-[2]分别会用后面的 4 # element、attributeName和value参数值进行替换,并执行该JavaScript代码 5 driver.execute_script("arguments[0].setAttribute\ 6 (arguments[1],arguments[2])", elementObj, attributeName, value)
封装好的setHTML
1 def setHTML(driver, elementObj, value): 2 #封装修改页面对象HTML代码的函数 3 driver.execute_script("arguments[0].innerText = '" + value + "'", elementObj)
封装好的HTML转markdown
1 def transform(a): 2 #将html翻译为markdown 3 return html2text.html2text(a)
chrome的一些设置
chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') #浏览器静默启动 chrome_options.add_argument('blink-settings=imagesEnabled=false') #浏览器无图模式
封装了Try,尝试次数和attribute的抓取器
def captureText(XPath,attribute="",reGetTimes = 100): for i in range(reGetTimes): try: get = driver.find_element_by_xpath(XPath) if(len(attribute)>0): get = get.get_attribute(attribute) get = str(get) if len(get) == 0: get = ' ' return str(get) else: return get.text except: sleep(0.1) return " "
对CSV进行操作
fp = open(fileName,"a",newline='') writer = csv.writer(fp) lines = [Title,releaseTime,author,description,views,likes,comments,tags] try: writer.writerow(lines) except: print('err') fp.close()
滑动到窗口底部
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')