Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
donggela
Python
提交
33f1eb58
P
Python
项目概览
donggela
/
Python
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
33f1eb58
编写于
5月 25, 2023
作者:
donggela
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
selenium模拟百度搜索
上级
e8fea871
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
53 addition
and
0 deletion
+53
-0
selenium_demo.py
selenium_demo.py
+53
-0
未找到文件。
selenium_demo.py
0 → 100644
浏览文件 @
33f1eb58
from
selenium
import
webdriver
from
selenium.webdriver.common.by
import
By
import
time
def
open_driver
():
option
=
webdriver
.
EdgeOptions
()
option
.
add_experimental_option
(
"detach"
,
True
)
option
.
add_experimental_option
(
'excludeSwitches'
,
[
'enable-automation'
])
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
edge
=
webdriver
.
Edge
(
options
=
option
)
edge
.
maximize_window
()
edge
.
get
(
"https://www.baidu.com"
)
# 判断“百度一下”在网页的标题上
assert
"百度一下"
in
edge
.
title
# ele = edge.find_element(By.ID,"kw")
# edge.find_element_by_id('kw')
return
edge
def
search
(
driver
):
input_ele
=
driver
.
find_element
(
By
.
ID
,
"kw"
)
input_ele
.
clear
()
input_ele
.
send_keys
(
"程序猿小董"
)
button
=
driver
.
find_element
(
By
.
ID
,
"su"
)
button
.
click
()
driver
.
implicitly_wait
(
10
)
# driver.find_elements(By.CLASS_NAME,"")
results
=
driver
.
find_elements
(
By
.
CSS_SELECTOR
,
".t a , em , .c-title-text"
)
for
result
in
results
:
if
result
.
get_attribute
(
"href"
):
if
"-"
not
in
result
.
get_attribute
(
"text"
):
# 搜索结果的标题
title
=
result
.
get_attribute
(
"text"
).
strip
()
# 搜索结果的网址
link
=
result
.
get_attribute
(
"href"
)
print
(
driver
.
title
)
print
(
driver
.
current_url
)
print
(
driver
.
window_handles
)
result
.
click
()
print
(
driver
.
window_handles
)
driver
.
switch_to
.
window
(
driver
.
window_handles
[
-
1
])
print
(
driver
.
title
)
print
(
driver
.
current_url
)
#滚动到目的元素
# ele = driver.find_element(By.CLASS_NAME,"tw0jb")
# driver.execute_script("arguments[0].scrollIntoView();",ele)
#向下滚动1000像素
driver
.
execute_script
(
"window.scrollBy(0,1000)"
)
time
.
sleep
(
60
)
driver
.
switch_to
.
window
(
driver
.
window_handles
[
0
])
print
(
driver
.
title
)
print
(
driver
.
current_url
)
driver
=
open_driver
()
search
(
driver
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录