![](/img/trans.png)
[英]TypeError: exe() missing 1 required positional argument: 'self'
[英]TypeError: Missing 1 required positional argument: 'self' Anybody?
我不斷遇到我的部分代碼的問題。 有人知道如何解決它嗎? Python - Selenium
我添加了完整的代碼。 目的是自動打開 Instagram 故事。
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import random
import string
#Chromedriver path. Make sure to have the same Chromedriver version as your Google Chrome browser
browser = webdriver.Chrome(executable_path= r"") # <----- ENTER PATH HERE
browser.get(('https://www.instagram.com/accounts/login/?source=auth_switcher'))
sleep(2)
def start():
acceptCookies = browser.find_element_by_xpath('/html/body/div[4]/div/div/button[1]');
acceptCookies.click();
sleep(4);
#browser.implicitly_wait(3) #this is another wait function.If you would like to run the script faster, change all sleep() to this
username = browser.find_element_by_name('username')
username.send_keys('') # <- INSERT YOUR INSTAGRAM USERNAME HERE -------------------------------------------------------------------------------------------------------------------------
password = browser.find_element_by_name('password')
password.send_keys('') # <- INSERT YOUR INSTAGRAM PASSWORD HERE -----------------------------------------------------------------------------------------------------------------------
nextButton = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div[1]/div/form/div/div[3]/button')
nextButton.click()
#browser.quit()
sleep(5)
notification = browser.find_element_by_xpath("//button[contains(text(), 'Niet nu')]")
notification.click()
sleep(5)
notification = browser.find_element_by_xpath("//button[contains(text(), 'Niet nu')]")
notification.click()
sleep(5)
def openStories(self):
bot=self.bot
bot.find_element_by_xpath('OE30K').click()
#Start the programm
start()
openStories()
在 Python 中self
是這樣使用的:
class MyClass:
def __init__(self, name: str, age: int) -> None: # constructor
self.age = age # self is the object that the constructor is initializing
self.myMethod(name) # You can call methods passing the instance itself as argument, the argument 'self' is the instance
def myMethod(self, name: str) -> None:
self.name = name
self
變量表示實例本身。
>>> my_object = MyClass()
>>> my_object.myMethod('myName')
>>> print(my_object.name)
'myName'
self
參數是調用方法的實例。
希望您了解在 class 中定義方法與在 class 之外定義方法之間的區別。
class MyClass:
foo = "HELLO!"
def openStories1(self):
# Here self is an instance of MyClass
print(self.foo)
def openStories2(self):
# Here self is just an object.
print(self.foo)
# Can be called without explicitly passing an argument, since self refers to the instance1
instance1 = MyClass()
instance1.openStories1()
# When defining outside a class you will need to pass the instance explicitly.
openStories2(instance1)
# Raises error since this function cannot be accessed through the instance because it's not defined in the class.
instance1.openStories2()
輸出:
HELLO!
HELLO!
Traceback (most recent call last):
File "scratch_6.py", line 18, in <module>
instance1.openStories2()
AttributeError: 'MyClass' object has no attribute 'openStories2'
我更新了代碼。 如果有人知道如何讓它工作,請幫我編輯代碼。 感謝社區!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.