我要创建一个自动登录名,并要向chromedriver添加扩展功能。我收到以下错误:“ TypeError:add_extension()缺少1个必需的位置参数:'self'”
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os
usr = "example"
passw = "password"
options = webdriver.ChromeOptions
options.add_extension(extension="[user]\OneDrive\Desktop\Python\mpbjkejclgfgadiemmefgebjfooflfhl")
br = webdriver.Chrome()
br.get("examplesite")
time.sleep(7)
user = br.find_element_by_css_selector("#login-username")
user.clear()
user.send_keys(usr)
passwd = br.find_element_by_css_selector("#login-password")
passwd.clear()
passwd.send_keys(passw)
btn = br.find_element_by_class_name("m-btn__text")
btn.click()
time.sleep(5)
value = br.find_element_by_class_name("value")
value.text()
time.sleep(100)
答案 0 :(得分:0)
您没有初始化webdriver.ChromeOptions
对象。试试:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os
usr = "example"
passw = "password"
options = webdriver.ChromeOptions()
options.add_extension(extension="[user]\OneDrive\Desktop\Python\mpbjkejclgfgadiemmefgebjfooflfhl")
br = webdriver.Chrome()
br.get("examplesite")
time.sleep(7)
user = br.find_element_by_css_selector("#login-username")
user.clear()
user.send_keys(usr)
passwd = br.find_element_by_css_selector("#login-password")
passwd.clear()
passwd.send_keys(passw)
btn = br.find_element_by_class_name("m-btn__text")
btn.click()
time.sleep(5)
value = br.find_element_by_class_name("value")
value.text()
time.sleep(100)
请注意options = webdriver.ChromeOptions
之后的括号。括号会创建一个新的ChromeOptions
对象;没有括号是对象本身的类别。