I am trying to write an auth bot with selenium, but I got an error that dont let code to be completed! The error is "AttributeError: 'str' object has no attribute 'start'", you can see the code and the error in terminal below.
Code: `
class LoginBot():
def __init__(self):
#Excel reading
wb = load_workbook('seo-fast_profiles.xlsx')
sheet_ranges = wb['Work']
self.login = sheet_ranges['A2'].value
self.email = sheet_ranges['B2'].value
self.passw = sheet_ranges['C2'].value
#Optins and UserAgent
user = UserAgent()
options = webdriver.ChromeOptions()
options.add_argument(user.random)
self.browser = webdriver.Chrome(service=ChromeDriverManager().install(), options=options)
def close_browser(self):
self.browser.close()
self.browser.quit()
def login(self):
browser = self.browser
browser.get("https://seo-fast.ru/login")
time.sleep(random.randrange(1, 3))
username_input = browser.find_element(By.ID, "logusername")
username_input.clear()
username_input.send_keys(self.email)
time.sleep(1)
username_input.send_keys(Keys.ENTER)
time.sleep(2)
password_input = browser.find_element(By.ID, "logpassword")
password_input.send_keys(self.passw)
time.sleep(5)
password_input.send_keys(Keys.ENTER)
time.sleep(7)
#cookies
pickle.dump(browser.get_cookies(), open(f"{self.login}_cookies", "wb"))
self.close_browser()
def xpath_exists(self, url):
browser = self.browser
try:
browser.find_element(By.XPATH, url)
exist = True
except NoSuchElementException:
exist = False
return exist
bot = LoginBot()
bot.login()
`
Error:
File "E:\Python\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__ self.service.start() AttributeError: 'str' object has no attribute 'start'
Can anyone solve it?
CodePudding user response:
You have missed the parameter. It should be like below.
self.browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
You need to import below library.
from selenium.webdriver.chrome.service import Service