1.click点击
2.跳转到新页面,selenium获取内容还停留在第一页,需要转句柄
3.截图不完整-->因为 browser.switch_to.window 会缩小窗口
4.iframe在转句柄后,关闭新页面还有重新进入iframe
遇见一个a标签没有url的网站,然后用click点击进行跳转,跳转后又selenium还停留在第一个页面,查找原因后用到了转句柄,转句柄后我的内容页for循环不能用了,结果发现iframe在句柄关闭后没有重新定位,解决这个问题后发现内容页截图不完整然后发现browser.switch_to.window会改变窗口大小文章来源:https://www.toymoban.com/news/detail-548420.html
import time
import uuid
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
uuid_list = []
uuid_list_ = []
def br():
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(options=chrome_options)
with open(r'C:\Users\xxx\Desktop\stealth.min.js') as f:
js = f.read()
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js})
browser.set_window_size(1200, 5000)
browser.set_page_load_timeout(15)
browser.set_script_timeout(15)
total(browser)
# content(browser)
browser.quit()
def total(browser):
browser.get(url='https://www.sprtc.com/index/yplxm.htm?activeName=wqzrxm&activeChildTab=')
time.sleep(1)
iframe1 = browser.find_element_by_xpath('//div[@class="center_list"]/iframe')
browser.switch_to.frame(iframe1)
browser.find_element_by_xpath('//*[@id="pane-wqzrxm"]/div[1]/div/label[2]/span').click()
title_total=browser.find_elements_by_xpath('//*[@id="pane-wqzrxm"]/div[2]/div[3]/table/tbody/tr')
link_total = browser.find_elements_by_xpath('//*[@id="pane-wqzrxm"]/div[2]/div[3]/table/tbody/tr/td[1]/div/span')
for i in title_total:
title_part=i.find_element_by_xpath('./td[1]/div/span').text
time_part=i.find_element_by_xpath('./td[4]/div/span').text
# link=i.find_element_by_xpath('./td[1]/div/span').click()
png_list=[]
for j in link_total:
handle = browser.current_window_handle
link_part=j.click() # 切换内容页面
time.sleep(0.5)
all_handles = browser.window_handles
new_handles = all_handles[1] # 从所有句柄的集合中,获取最后那个,也就是最新的了
browser.switch_to.window(new_handles) # 然后当参数传递进来
browser.set_window_size(1200, 5000)
now_handles = browser.current_window_handle # 查看现在的句柄
# content_1=browser.find_element_by_xpath('//div[@class="content"]').text
for k in range(6):
browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
time.sleep(1)
screenshot_content = browser.find_element_by_xpath('//*[@id="iframe"]')
uid = str(uuid.uuid4())
# uuid_list_.append(uid)
suid = ''.join(uid.split('-'))
browser.get_screenshot_as_file('D:\PyCharmWorkSpace\shiyan\A\%s.png' % suid)# 对浏览器截图
left = screenshot_content.location['x']
top = screenshot_content.location['y']
right = screenshot_content.location['x'] + screenshot_content.size['width']
bottom = screenshot_content.location['y'] + screenshot_content.size['height']
img = Image.open('D:\PyCharmWorkSpace\shiyan\A\%s.png' % suid)
img = img.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪
img.save('D:\PyCharmWorkSpace\shiyan\B\%s.png' % suid)
# uuid_list.append(uuid_list_)
png_list.append('D:\PyCharmWorkSpace\shiyan\B\%s.png' % suid)
browser.close()
browser.switch_to.window(handle)
iframe1 = browser.find_element_by_xpath('//div[@class="center_list"]/iframe')
browser.switch_to.frame(iframe1)
if __name__ == '__main__':
br()
后续再优化文章来源地址https://www.toymoban.com/news/detail-548420.html
到了这里,关于selenium click点击 跳转新页面转句柄的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!