Python 和 Selenium 代码错误 - 自动化未按预期收集数据

问题描述 投票:0回答:0

一个。我有一个简单的自动化代码,我想从中学习。该代码是一个机器人软件,可以找到有关重要科学家的关键信息并将其显示给用户。

b。该程序自我介绍并解释将要采取的步骤。

c。它导航到在科学家列表中找到的科学家的维基百科页面。

d。它检索科学家出生和死亡的日期并计算他们的年龄。此外,检索他们的维基百科页面的第一段。

e。它以易于理解的方式向用户显示所有这些信息。

f.我不想使用维基百科的任何 API

这是我文件夹中的内容:

在我的main.py中:

from robotics import Robot
from RPA.Browser.Selenium import Selenium as ChromeBrowser
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

SCIENTISTS = ["Albert Einstein", "Isaac Newton",
              "Marie Curie", "Charles Darwin"]

robot = Robot("WikiRobot")


def introduce_yourself():
    robot.say_hello()


def main():
    robot = Robot("WikiRobot")
    robot.say_hello()

    driver = webdriver.Chrome()

    for scientist in SCIENTISTS:
        # navigate to wikipedia page
        driver.get(
            f"https://en.wikipedia.org/wiki/{scientist.replace(' ', '_')}")

        try:
            # wait for the birthDate element to be visible
            birth_date_element = WebDriverWait(driver, 10).until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, "span.bday"))
            )
            birth_date = birth_date_element.text

            death_date_element = WebDriverWait(driver, 10).until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, "span.dday"))
            )
            death_date = death_date_element.text

            born = datetime.strptime(birth_date, '%Y-%m-%d')
            died = datetime.strptime(death_date, '%Y-%m-%d')
            age = int((died - born).days / 365.25)

            summary = WebDriverWait(driver, 10).until(
                EC.visibility_of_element_located(
                    (By.XPATH, "//div[@id='mw-content-text']/div[1]/p[1]"))
            ).text

            # store information in dictionary
            scientist_info = {
                "Name": scientist,
                "Birth Date": birth_date,
                "Death Date": death_date,
                "Age": age,
                "Summary": summary
            }

            # display information
            for key, value in scientist_info.items():
                print(f"{key}: {value}")
                sleep(1)

        except TimeoutException as e:
            print(f"An error occurred while processing {scientist}: {e}")

    # Close the browser after the loop
    driver.quit()


if __name__ == "__main__":
    main()

在我的robotics.py中:
xx

from RPA.Browser.Selenium import Selenium as ChromeBrowser


class Robot:
    def __init__(self, name):
        self.name = name
        self.browser = ChromeBrowser()

    def say_hello(self):
        print("Hello, my name is " + self.name)

    def say_goodbye(self):
        print("Goodbye, my name is " + self.name)

    def open_webpage(self, webpage):
        self.browser.get(webpage)

到目前为止,当我运行代码时,在终端中运行 python main.py,该程序确实在 Chrome 中一个接一个地经过科学家,当它关闭时,不幸的是,我没有得到我想在终端。因此,我留下了包含的错误消息:

大家好,我叫维基机器人

处理阿尔伯特·爱因斯坦时发生错误:消息:

处理 Isaac Newton 时发生错误:消息:

处理居里夫人时发生错误:消息:

处理 Charles Darwin 时发生错误:消息:

python selenium-webdriver automation selenium-chromedriver rpa
© www.soinside.com 2019 - 2024. All rights reserved.