首页 > 解决方案 > 使用 selenium 的 Web 驱动程序获取冠状病毒数据的问题

问题描述

我正在编写一个 Python 脚本来使用 selenium 的 webdriver从Wordlometers获取冠状病毒数据。我的脚本由以下组成:

class Coronavirus():
    def __init__(self):
        options = webdriver.ChromeOptions()
        options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
        chrome_driver_binary = "/usr/local/bin/chromedriver"
        self.driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)

    def get_data(self):
        try:
            self.driver.get('https://www.worldometers.info/coronavirus/')
            table = self.driver.find_element_by_xpath('//*[@id="main_table_countries"]/tbody[1]')
            country_element = table.find_element_by_xpath("//td[contains(text(), 'Italy')]")
            row = country_element.find_element_by_xpath("./..")

但发生的是 Chrome 会立即打开和关闭。我尝试调试这个脚本,在这里和那里添加一些打印,结果证明该行self.driver.find_element_by_xpath不允许我打印任何其他内容。我尝试阅读文档,但无法打印任何内容,我该怎么做才能解决这个问题?
非常感谢!

标签: pythonseleniumselenium-chromedriver

解决方案


推荐阅读