pythonseleniumvisual-studio-codemechanicalsoup

Trying to read data from Kaspersky website with MechanicalSoup or Selenium


Currently, I'm trying to scrape data from a Website (https://account.kaspersky.com/). Before I can read the data I need to login to the website. But for some reason, it is not working. I read through the internet to get it to work, but unfortunately, I wasn't able to solve the issue.

import mechanicalsoup
import csv
import xlsxwriter

from time import sleep

# create stateful browser
browser = mechanicalsoup.StatefulBrowser(
    soup_config={'features': 'lxml'},
    raise_on_404=True,
    user_agent='MyBot/0.1: mysite.example.com/bot_info',
)

# use browser to open link
browser.open("https://account.kaspersky.com/")

sleep(2)

# check url
print(browser.get_url())

# get first form available
form = browser.select_form()

browser.submit()

# check url
print(browser.get_url())

The script always ends at the selct_form() method. No matter what I try I always get the same error. Even when I specify it.

Traceback (most recent call last):
File "c:\Python\Cloud.Kaspersky\read_from_website_mechanicalsoup.py", line 23, in <module>
form = browser.select_form()
File "C:\Users\dw.FROMMEDV\AppData\Local\Programs\Python\Python39\lib\site-packages\   
      mechanicalsoup\stateful_browser.py", line 220, in select_form
raise LinkNotFoundError()
mechanicalsoup.utils.LinkNotFoundError

After a few hours of trying, I wanted to try a different tool. Selenium. But I have kind of the same problem here as well. But here I can't submit the login. Selenium can't find the button.

from selenium import webdriver
import time

driver = webdriver.Firefox()

driver.get("https://account.kaspersky.com/")

file = open('login.txt', 'r')
username_file = file.readline()
password_file = file.readline()

time.sleep(1) 

username = driver.find_element_by_id("EMail")
username.clear()
username.send_keys(username_file)

time.sleep(1) 

password = driver.find_element_by_name("Password")
password.clear()
password.send_keys(password_file)

time.sleep(2) 

driver.find_element_by_class_name("assets-button primary").click()

Is it possible that this website is protected or something? Or is anyone seeing my issue?

Here the Error Message with Selenium:

Traceback (most recent call last):
File "c:\Python\Cloud.Kaspersky\read_from_website_selenium.py", line 26, 
in
<module>
driver.find_element_by_class_name("assets-button primary").click()
File "C:\Users\dw.FROMMEDV\AppData\Local\Programs\Python\Python39
\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 564, in 
find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\dw.FROMMEDV\AppData\Local\Programs\Python\Python39  
\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in
find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\dw.FROMMEDV\AppData\Local\Programs\Python\Python39
\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)

File "C:\Users\dw.FROMMEDV\AppData\Local\Programs\Python\Python39 
\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 242, in 
check_response

raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.NoSuchElementException: Message: Unable to  
locate
element: .assets-button primary

Solution

  • Try using the following identifiers. The last line is the error using Java instead of Python and multiple class names instead of singular.

    driver.find_element_by_class_name("assets-button").click()
    
    driver.find_element_by_xpath("//button[@class='assets-button primary']").click()
    

    You could also try

    from selenium.webdriver.common.keys import Keys
    password.send_keys(Keys.ENTER)