0

目前,我正在尝试从网站(https://account.kaspersky.com/)中抓取数据。在我可以读取数据之前,我需要登录网站。但由于某种原因,它不起作用。我通过互联网阅读以使其正常工作,但不幸的是,我无法解决这个问题。

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())

脚本总是以 selct_form() 方法结束。无论我尝试什么,我总是会遇到同样的错误。即使我指定它。

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

经过几个小时的尝试,我想尝试不同的工具。硒。但我在这里也有同样的问题。但是在这里我无法提交登录。Selenium 找不到按钮。

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()

这个网站有可能受到保护吗?或者有人看到我的问题吗?

这是 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
4

1 回答 1

0

尝试使用以下标识符。最后一行是使用 Java 而不是 Python 和多个类名而不是单数的错误。

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

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

你也可以试试

from selenium.webdriver.common.keys import Keys
password.send_keys(Keys.ENTER)
于 2021-03-15T11:13:49.380 回答