# 导入模块
from selenium.webdriver import Chrome
# 导入键盘
from selenium.webdriver.common.keys import Keys
# 导入时间模块
import time
# 创建一个浏览器
web = Chrome()
# 打开一个网址
web.get("http://xxxxxxxxx.com")
# 找到某个元素. 点击它
el = web.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[1]/a')
el.click() # 点击
time.sleep(1) # 让浏览器缓一会儿
# 找到输入框. 输入要搜索的内容 python => 输入回车/点击搜索按钮
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER) # 直接就回车了ENTER文章来源:https://www.toymoban.com/news/detail-778548.html
time.sleep(1)
# 查找存放数据的位置. 进行数据提取
# 找到页面中存放数据的所有的li
lis = web.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')
for li in lis:
name = li.find_element_by_tag_name("h3").text
price = li.find_element_by_xpath("./div/div/div[2]/div/span").text
company = li.find_element_by_xpath('./div/div[2]/div/a').text
print(company, name, price)
文章来源地址https://www.toymoban.com/news/detail-778548.html
到了这里,关于selenium自动化的输入和回车操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!