用python代码实现对tpshop商城的搜索框进行封装,然后进行调用
时间: 2024-06-10 15:05:57 浏览: 8
好的,下面是使用Python对tpshop商城的搜索框进行封装的代码:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class TpshopSearch:
def __init__(self, driver):
self.driver = driver
def search(self, keyword):
self.driver.get("http://www.tpshop.com/")
search_box = self.driver.find_element_by_id("q")
search_box.clear()
search_box.send_keys(keyword)
search_box.send_keys(Keys.RETURN)
```
使用方法:
```python
driver = webdriver.Chrome()
tpshop_search = TpshopSearch(driver)
tpshop_search.search("手机")
```
这个代码使用了Selenium WebDriver库来操作浏览器,实现了对tpshop商城搜索框的封装。其中,我们通过find_element_by_id方法找到了id为"q"的搜索框,然后通过send_keys方法输入搜索关键字并按回车键进行搜索。通过封装,我们可以在不了解内部实现细节的情况下,轻松实现商城搜索的功能。