简体   繁体   English

使用 selenium python 右键单击网页后从 csv 抓取数据

[英]Scrape data from csv downloaded after right clicking on webpage using selenium python

I'm looking to scrape data from a webpage using python and selenium.我正在寻找使用 python 和 selenium 从网页中抓取数据。 There is a csv download option which is visible only after a right click in the frame of the graph.有一个 csv 下载选项,只有在图形框架中单击鼠标右键后才能看到该选项。 I am not able to right click on the page and click on csv - download option using selenium.我无法右键单击页面并单击 csv - 使用 selenium 下载选项。 Here is the link for web page from where I am trying to get data - https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/page/Z3ToB Have tried following set of code to do that:这是我试图从中获取数据的 web 页面的链接 - https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/

options = webdriver.ChromeOptions()
    options.binary_location = r"<Path where chrome application is installed>"
    driver = webdriver.Chrome(r"<path to chrome driver>",chrome_options=options)
    driver.get("https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/page/Z3ToB")
    timeout = 10
    from selenium.webdriver import ActionChains
    action = ActionChains(driver)
    action.move_to_element(driver.find_element_by_xpath("//lego-canvas-container[@class='lego-canvas-container']")).perform()
    action.context_click().perform()

Using this, not able to find the given XPATH, even tried with class name like report area.使用它,无法找到给定的 XPATH,甚至尝试使用 class 名称(如报告区域)。 Could anyone guide about how to right click on anywhere in the frame and then find the download csv option within that?谁能指导一下如何右键单击框架中的任何位置,然后在其中找到下载 csv 选项?

Because of javascript going to visible after right click its unable to find xpath without right click try this code its worked on me由于 javascript 在右键单击后可见,因此没有右键单击无法找到 xpath 尝试此代码对我有用

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-blink-features=AutomationControlled') 
driver = webdriver.Chrome(executable_path = 'chromedriver.exe',options = chrome_options)
driver.implicitly_wait(10)
driver.get("https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/page/Z3ToB")
action = ActionChains(driver)
action.pause(1)
action.move_by_offset(150,150).perform()
action.context_click().perform()
action.move_to_element(driver.find_element_by_xpath('//*[@id="mat-menu-panel-0"]/div/span[5]/button')).perform()
action.click().perform()

Use the below xpath to identify the element and then right click and then find the csv button and click.使用下面的xpath识别元素,然后右键单击,然后找到 csv 按钮并单击。

driver.get("https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/page/Z3ToB")
time.sleep(5) #delay to load page properly. you can use explicit wait as well
element=driver.find_element_by_xpath("//div[@class='drop-zone-text']")
action = ActionChains(driver)
action.move_to_element(element).perform()
action.context_click().perform()
#To click on download csv
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Download CSV')]"))).click()

You need to import below libraries您需要导入以下库

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

Browser snapshot:浏览器快照:

在此处输入图像描述

I found it a bit easier to right click and then use the arrow keys to select the appropriate option.我发现右键单击然后使用箭头键 select 适当的选项更容易一些。

So you can perform a right click/context_click anywhere on the canvas, to open the menu popup.因此,您可以在 canvas 的任意位置执行right click/context_click ,以打开菜单弹出窗口。 Then you can move up and down with arrow keys and select the 'Download Csv' option.然后您可以使用箭头键和 select 上下移动“下载 Csv”选项。

actions = ActionChains(driver)

# Find the canvas element
element = driver.find_element_by_xpath('//*[@id="body"]/div/div/div[1]/div[2]/div/div[1]/div[1]/div[1]/div/lego-report/lego-canvas-container/div/file-drop-zone/span/content-section/div[3]/canvas-component')

# Right click the element, then press the Down key twice followed by the Enter to move to the Download CSV option and select it.
actions.move_to_element(element).context_click().send_keys([Keys.DOWN, Keys.DOWN, Keys.ENTER]).perform()

driver.get("https://datastudio.google.com/reporting/d97f5736-2b85-4f39-beba-6dc386c24429/page/Z3ToB")
time.sleep(5)
source= wait.until(EC.presence_of_element_located((By.XPATH,"/html/body")))
action = ActionChains(driver)
action.context_click(source).perform()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#mat-menu-panel-0 > div > span:nth-child(5) > button"))).click()

Weirdly got it to work with this.奇怪的是让它与这个一起工作。 Seems you need to wait and the context click the body and then click on the menu element.似乎您需要等待,上下文单击正文,然后单击菜单元素。

<button _ngcontent-fys-c1="" class="mat-focus-indicator mat-tooltip-trigger mat-menu-item ng-star-inserted" mat-menu-item="" role="menuitem" tabindex="0" aria-disabled="false"> Download CSV <!----><!----><!----><div class="mat-menu-ripple mat-ripple" matripple=""></div></button>

Import进口

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 中的 Selenium 从在滚动上添加 div 的网页中抓取数据? - How do I scrape data using Selenium in Python from a webpage that adds div on scroll? 无法在python中使用selenium抓取动态网页 - Failing to scrape dynamic webpage using selenium in python 如何使用 Python Selenium 将数据从脚本元素 (HTML) 抓取到 CSV - How to scrape data from a Script element (HTML) to a CSV using Python Selenium Python-使用Selenium从网页提取数据 - Python - Using Selenium to extract data from Webpage 使用美丽的汤从网页中的链接中抓取数据。 Python - Scrape data from a link in a webpage using beautiful soup. python 使用 Selenium Python Beautifulsoup 在网页上单击按钮 - Clicking button on webpage using Selenium Python Beautifulsoup 使用python登录后如何抓取网页? - how to scrape a webpage after a login using python? 刷新后如何从网页中抓取数据 - How to scrape data from a webpage after refresh 在使用 python selenium 单击查看联系人后,我无法获取相同的检查 output - i can't scrape mobile no as getting same inspect output after clicking on view contact using python selenium 如何从在 Python 中使用 react.js 和 Selenium 的网页抓取数据? - How to scrape data from webpage which uses react.js with Selenium in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM