您的位置:首页 > 文旅 > 美景 > selenium实现点击或回车搜索页面:以JD为例

selenium实现点击或回车搜索页面:以JD为例

2024/10/6 12:31:19 来源:https://blog.csdn.net/2301_79675943/article/details/139597221  浏览:    关键词:selenium实现点击或回车搜索页面:以JD为例

记录一个实现搜索功能的爬虫

from selenium import webdriver  
from selenium.webdriver.common.by import By  
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support import expected_conditions as EC  
from selenium.webdriver.common.keys import Keys
# 创建WebDriver
driver = webdriver.Firefox()  # 打开网页  
driver.get("https://www.jd.com/?cu=true")  # 替换为你要搜索的网站地址  # 定位搜索框并输入关键词  
search_box_xpath = '//*[@id="key"]'  # 替换为实际的搜索框XPath  
search_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, search_box_xpath)))  
search_box.send_keys("替换为要搜索的关键词")  # 替换为要搜索的关键词  # 触发搜索
# 方法一:按下回车键触发搜索  
search_box.send_keys(Keys.RETURN)# 方法二:点击搜索按钮触发搜索(如果有搜索按钮的话)  
# search_button_xpath = "/html/body/div[1]/div[4]/div/div[2]/div/div[2]/button"  # 替换为实际的搜索按钮XPath  
# search_button = driver.find_element(By.XPATH, search_button_xpath)  
# search_button.click()  # 关闭浏览器(在完成所有操作后)  
# driver.quit()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com