猿问

为什么我无法从PowerBI网页中读取蟒蛇中硒的x路径

我正在尝试从巴西所在州的网页上阅读COVID-19确诊病例的数量,但该页面确实必须从中获取数据。这是一个 PowerBI 网页,案例数位于演示文稿的第二页上。我可以正常地阅读第一页的任何内容,但我似乎无法阅读第二页的任何内容(在我单击按钮切换到下一页后,该页面位于页面的脚注中)。我的代码是这样的


from selenium import webdriver

from time import sleep

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


class PowerBIBot:

    def __init__(self):

        self.driver = webdriver.Chrome()

        self.driver.get("https://app.powerbi.com/view?r=eyJrIjoiMDgwOGI4YjItNGFjNC00ZThkLWIyNzctMmNjZTQxMmU1ZjRhIiwidCI6Ijg3ZTRkYTJiLTgyZGYtNDhmNi05MTU3LTY5YzNjYTYwMGRmMiIsImMiOjR9&fbclid=IwAR1U64ZAVQ0IZ9RkiZnO7K7ysbvGtAGHCJWqIbIG8Z7SBfcM8hLSv7B2JSU")

        sleep(8)

        test = self.driver.find_element_by_xpath("/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-legacy/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[3]/transform/div/div[3]/visual-modern/div/div")

        # This works

        print(test)

        self.driver.find_element_by_xpath('/html/body/div[1]/ui-view/div/div[2]/logo-bar/div/div/div/logo-bar-navigation/span/a[3]/i') \

            .click()

        sleep(8)

        try:

            element = WebDriverWait(self.driver, 25).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-legacy/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[3]/transform/div/div[3]/visual-modern/div/svg/g[1]/text")))

            print(element)

        finally:

            pass


PowerBIBot()

在那里,你有我试图自动阅读的网站的URL。我知道代码不是很好,但我只是想了解硒是如何工作的,以及我如何阅读这些PowerBI页面,由于某种原因,这些页面真的很难阅读。我尝试等待很长时间才能加载页面,但它从未起作用。任何帮助将不胜感激。


RISEBY
浏览 63回答 1
1回答

茅侃侃

请尝试以下代码:#click next pageWebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i[title='Next Page']"))).click()try:    elements = WebDriverWait(self.driver, 25).until(EC.presence_of_all_elements_located((By.XPATH, "//*[@class='card' and @aria-label != '']")))    for element in elements:        print(element.get_attribute('aria-label'))finally:    pass
随时随地看视频慕课网APP

相关分类

Python
我要回答