当前位置:网站首页>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
边栏推荐
- Small volume Schottky diode compatible with nsr20f30nxt5g
- 简单的拖拽物体到物品栏
- Improving 3D object detection with channel wise transformer
- Supplement: Annotation
- 做数据可视化应该避免的8个误区
- Recommended scheme for national production of electronic components of wireless keyboard
- leetcode007--判断字符串中的括号是否匹配
- Supplément: annotation
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- 2019 is coming to an end, the longest day.
猜你喜欢
Use recyclerview to realize left-right side-by-side classification selection
Supplement 14: cmake practice project notes (to be continued 4 / 22)
383. 赎金信
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
補:注解(Annotation)
What is a data island? Why is there still a data island in 2022?
229. Find mode II
Coinbase: basic knowledge, facts and statistics about cross chain bridge
New terminal play method: script guidance independent of technology stack
Unity RawImage背景无缝连接移动
随机推荐
Leetcode009 -- search the target value in the array with binary search
[paper reading] [3D object detection] voxel transformer for 3D object detection
Use recyclerview to realize left-right side-by-side classification selection
補:注解(Annotation)
递归调用--排列的穷举
Leetcode001 -- returns the subscript of the array element whose sum is target
shell wc (统计字符数量)的基本使用
Recommended scheme of national manufactured electronic components for intelligent electronic scales
What is a data island? Why is there still a data island in 2022?
Supplément: annotation
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
协程与多进程的完美结合
La caméra Unity tourne avec la souris
Leetcode - > 1 sum of two numbers
Iron and intestinal flora
2021数学建模国赛一等奖经验总结与分享
QML进阶(四)-绘制自定义控件
A lifetime of needs, team collaboration can play this way on cloud nailing applet
A new method for evaluating the quality of metagenome assembly - magista
RC低通滤波器的逆系统