想要用python实现自动打开小红书进行自动点赞,然后就跟着网上的代码敲,代码未编写完,想先运行前部分代码看看能不能打开,但是发现一直有错
selenium.common.exceptions.WebDriverException: Message: Desired Capabilities must be a dictionary
import time
import datetime
import pymysql
from appium import webdriver
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
#driver = webdriver.Chrome("chromedriver.exe")
#driver = webdriver.Chrome("C:\Program Files\Python311\Scripts")
#启动app
caps = {
"platformName", "Android",
"deviceName", "nova7",
"appPackage", "com.xingin.xhs",
"platformVersion", "7",
"appActivity", ".index.v2.IndexActivityV2", #主页
"ensureWebviewsHavePages", True,
"nativeWebScreenshot", True,
"newCommandTimeout", 3600,
"connectHardwareKeyboard", True
}
#获取屏幕尺寸
def getSize():
x = driver.get_window_size()['width'] #宽
y = driver.get_window_size()['height'] #高
return (x,y)
#判断是否存在指定元素
def isElement(driver,identifyBy,c):
time.sleep(1)
flag = False
try:
if identifyBy == "id":
driver.find_element_by_id(c) #定位id?
elif identifyBy == "xpath":
driver.find_element_by_xpath(c) #定位
elif identifyBy == "class":
driver.find_element_by_class(c)
elif identifyBy == "link text":
driver.find_element_by_link_text(c)
elif identifyBy == "partial link text":
driver.find_element_by_partial_link_text(c)
elif identifyBy == "name":
driver.find_element_by_name(c)
elif identifyBy == "tag name":
driver.find_element_by_tag_name(c)
elif identifyBy == "css selector":
driver.find_element_by_css_selector(c)
flag = True
except Exception as e:
#不存在数据
flag = False
finally:
return flag
if __name__ =='__main__':
#打开app
#driver = webdriver.Chrome("C:\Program Files\Python311\Scripts")
driver =webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
print('当前时间',datetime.datetime.now())
xx = getSize()
print('屏幕尺寸',xx)
#点击首页输入框
t = driver.find_element_by_id("com.xingin.xhs:id/fdk").click()
#driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.widget.LinearLayout/android.widget.RelativeLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ImageView
#")
time.sleep(2)
#输入查询数据
driver.find_element_by_id("com.xingin.xhs:id/dl1").send_keys('675011952')
#driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.EditText")
time.sleep(2)
#点击搜索按钮
driver.find_element_by_id("com.xingin.xhs:id/dl6").click()
#driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.TextView")
time.sleep(2)
#点击用户
driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/androidx.appcompat.app.ActionBar.Tab[2]/android.widget.TextView').click()
time.sleep(2)
#点击进入用户主页
driver.find_element_by_id("com.xingin.xhs:id/dl7").click()
#driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout/android.view.View")
time.sleep(2)
driver.quit()
'''
#判断是否有笔记
def getnote():
time.sleep(1)
header = {
"User-Agent":""
"cookie":""
}
driver.find_element_by_id("com.xingin.xhs:id/dl7")
'''
问题
咋解决哇
D:\ABB\Python\Python\pycham\pypro10\venv\Scripts\python.exe D:\ABB\Python\Python\pycham\pypro10\duqu.py
D:\ABB\Python\Python\pycham\pypro10\duqu.py:75: DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg
driver =webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
Traceback (most recent call last):
File "D:\ABB\Python\Python\pycham\pypro10\duqu.py", line 75, in
driver =webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ABB\Python\Python\pycham\pypro10\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 268, in init
raise WebDriverException("Desired Capabilities must be a dictionary")
selenium.common.exceptions.WebDriverException: Message: Desired Capabilities must be a dictionary
```
caps 得要定义成字典格式,你这个既不是字典,也不是list,
将键值中间的逗号改成冒号,键值对应
caps = {
"platformName": "Android",
"deviceName": "nova7",
"appPackage": "com.xingin.xhs",
"platformVersion": "7",
"appActivity": ".index.v2.IndexActivityV2", #主页
"ensureWebviewsHavePages": True,
"nativeWebScreenshot": True,
"newCommandTimeout": 3600,
"connectHardwareKeyboard": True
}