更新后selenium自动化点击搜索按钮

该代码示例展示了如何在更新后的Selenium中使用find_element和find_elements方法。通过Edge浏览器,定位淘宝搜索框(By.ID:q)输入iphone,然后找到搜索按钮(By.XPATH)并点击执行搜索。程序等待一段时间后关闭浏览器。

 现在selenium改了后只有find_element和find_elements两种函数,所以我们现在定位和点击导入

from selenium.webdriver.common.by import By

 以淘宝为例定位标签我们使用By.ID,搜索按钮我们使用By.XPATH

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from time import sleep

s = Service('./msedgedriver.exe')
bro = webdriver.Edge(service_args=s)

bro.get('https://www.taobao.com/')

# 定位标签
taobiao_input = bro.find_element(By.ID, value='q')
taobiao_input.send_keys('iphone')
sleep(2)

# 定位搜索按钮并点击
search_button = bro.find_element(By.XPATH,'//*[@id="J_TSearchForm"]/div[1]/button')
search_button.click()


sleep(5)
bro.quit()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值