当前位置:网站首页>selenium模式下切换窗口,抓取数据的实现
selenium模式下切换窗口,抓取数据的实现
2022-04-23 04:43:00 【子嘉113】
selenium下切换窗口的方法如下
for handle in driver.window_handles:
driver.switch_to_window(handle)
具体应用可以测试如下实例(代码如下):
from selenium import webdriver
import time
import random
import csv
# 1.绕过滑块验证
inquire_company=input('请输入要查询的公司名称:')
option=webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
driver=webdriver.Chrome(chrome_options=option)
# 打开网址
url='https://www.qcc.com/'
driver.get(url=url)
driver.find_element_by_xpath('//*[@id="searchKey"]').send_keys(inquire_company)
# driver.find_element_by_xpath('/html/body/div/div[2]/section[1]/div/div/div/div[1]/div/div/span/button').click()
# 点击查询
button=driver.find_element_by_xpath('/html/body/div/div[2]/section[1]/div/div/div/div[1]/div/div/span/button')
driver.execute_script("arguments[0].click();", button)
# driver.implicitly_wait(60)
# 点击第一个搜索结果
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[3]/div/div[2]/div/table/tr[1]/td[3]/div/div[1]/span[1]/a').click()
# driver.implicitly_wait(30)
# 点出新的窗口,需要切换窗口
for handle in driver.window_handles:
driver.switch_to_window(handle)
# text2=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[3]/td[2]/div/div/span[2]/span/a').size
with open('企业信息.csv','w')as f:
headers1=['企业名称','对外投资']
control_company=driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div/span[1]/h1').text
headers2=[control_company,'序号','被投资企业名称','法定代表人','注册资本','成立日期','状态','持股比例','认缴出资额']
f_csv=csv.writer(f)
f_csv.writerow(headers1)
f_csv.writerow(headers2)
try:
for i in range(2,100):
company_name=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[2]/div/div/span[2]/span/a'.format(i)).text
company_role=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[3]/div/div/span/span/a'.format(i)).text
company_money=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[4]/div/span'.format(i)).text
bulild_time=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[5]/div/span'.format(i)).text
company_state=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[6]/span/span'.format(i)).text
control_per=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[7]/div/span'.format(i)).text
out_money=driver.find_element_by_xpath('//*[@id="touzilist"]/div[2]/table/tr[{}]/td[8]/div/span'.format(i)).text
content=[' ',i-1,company_name,company_role,company_money,bulild_time,company_state,control_per,out_money]
f_csv.writerow(content)
print('第{}个控制公司是:'.format(i)+company_name)
except :
pass
driver.quit()
# print(text2)
一个可以直接测试的应用实现,配置好环境可以直接测试,有问题可以留言讨论。
遇到的bug总结:
bug1:
selenium.common.exceptions.WebDriverException: Message: element click intercepted: Element … is not clickable at point (201, 298). Other element would receive the click:
解决办法:
将原代码
driver.find_element_by_xpath(‘//[@id=“login-form”]/div[5]/button’).click
改为
button=driver.find_element_by_xpath('//[@id=“login-form”]/div[5]/button’)
driver.execute_script(“arguments[0].click();”, button)
版权声明
本文为[子嘉113]所创,转载请带上原文链接,感谢
https://blog.csdn.net/luluzsa/article/details/124333480
边栏推荐
- 华为机试--高精度整数加法
- AWS eks add cluster user or Iam role
- Spark case - wordcount
- C language: Advanced pointer
- leetcode005--原地删除数组中的重复元素
- MySQL queries users logged in for at least N consecutive days
- Coinbase:关于跨链桥的基础知识、事实和统计数据
- Logger and zap log Library in go language
- [pytoch foundation] torch Split() usage
- Unity3d practical skills - theoretical knowledge base (I)
猜你喜欢
Coinbase:关于跨链桥的基础知识、事实和统计数据
Chapter 4 - understanding standard equipment documents, filters and pipelines
Jetpack 之 LifeCycle 组件使用详解
Unity RawImage背景无缝连接移动
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Go reflection rule
补充番外14:cmake实践项目笔记(未完待续4/22)
win10, mysql-8.0.26-winx64. Zip installation
简单的拖拽物体到物品栏
随机推荐
leetcode006--查找字符串数组中的最长公共前缀
Case of using stream load to write data to Doris
New terminal play method: script guidance independent of technology stack
Mysql50 basic exercises
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
leetcode002--将有符号整数的数字部分反转
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Basic use of shell WC (counting the number of characters)
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Leetcode001 -- returns the subscript of the array element whose sum is target
Apache Bench(ab 压力测试工具)的安装与使用
2019 is coming to an end, the longest day.
AWS EKS 部署要点以及控制台与eksctl创建的差异
从MySQL数据库迁移到AWS DynamoDB
520. Detect capital letters
Leetcode004 -- Roman numeral to integer
Solutions to the failure of sqoop connection to MySQL
QML进阶(四)-绘制自定义控件
Use recyclerview to realize left-right side-by-side classification selection
shell wc (统计字符数量)的基本使用