【问题标题】:Select option after right click with Selenium on PowerBi site在 PowerBi 网站上用 Selenium 右键单击​​后选择选项
【发布时间】:2020-05-11 21:31:46
【问题描述】:

我有这个 Python 代码来抓取一个 PowerBI 网站:

driver = webdriver.Firefox()
driver.get("https://app.powerbi.com/view?r=eyJrIjoiZGYxNjYzNmUtOTlmZS00ODAxLWE1YTEtMjA0NjZhMzlmN2JmIiwidCI6IjljOWEzMGRlLWQ4ZDctNGFhNC05NjAwLTRiZTc2MjVmZjZjNSIsImMiOjh9")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='navigation-wrapper navigation-wrapper-big']//i[@title='Next Page']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='navigation-wrapper navigation-wrapper-big']//i[@title='Next Page']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='navigation-wrapper navigation-wrapper-big']//i[@title='Next Page']"))).click()

action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath("/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/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[9]/transform")).perform()
action.context_click().perform()
action.move_to_element(driver.find_element_by_xpath("/html/body/div[7]/drop-down-list/ng-transclude/ng-repeat[1]")).perform()
action.click().perform()
driver.quit()

上面的代码让我明白了这一点:

代码的最后两行应该点击Show as a table,但这并没有发生。如何点击?

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver web-scraping powerbi


    【解决方案1】:

    要点击Show as a table,请使用以下xpath。我已经测试了几次,它工作正常。

    element=WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//div[@title='Show as a table']")))
    

    代码

    driver = webdriver.Firefox()
    driver.get("https://app.powerbi.com/view?r=eyJrIjoiZGYxNjYzNmUtOTlmZS00ODAxLWE1YTEtMjA0NjZhMzlmN2JmIiwidCI6IjljOWEzMGRlLWQ4ZDctNGFhNC05NjAwLTRiZTc2MjVmZjZjNSIsImMiOjh9")
    for i in range(4):
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='navigation-wrapper navigation-wrapper-big']//i[@title='Next Page']"))).click()
    
    action = ActionChains(driver)
    action.move_to_element(driver.find_element_by_xpath("/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/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[9]/transform")).perform()
    action.context_click().perform()
    element=WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//div[@title='Show as a table']")))
    action.move_to_element(element).click().perform()
    

    【讨论】:

    • 那行得通。我唯一注意到的是action.move_to_element(element).click().perform() 也打开了右键菜单。我认为这是因为action.context_click().perform()。不知道有没有办法在执行最后一行的时候阻止右键菜单?
    • 好的。我会检查并让您知道我是否有答案?同时你可以试试这个来检查它是否有效action.send_keys(Keys.ESCAPE).perform()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多