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

1053

被折叠的 条评论
为什么被折叠?



