sea*_*son 3 python selenium svg web-scraping webdriverwait
我正在尝试抓取从鼠标悬停事件动态生成的数据。
我想从https://slushpool.com/stats/?c=btc的哈希率分布图表中捕获信息,该图表是在滚动每个圆圈时生成的。
下面的代码从网站获取 html 数据,并返回鼠标经过一个圆圈时填充的表格。但是,我无法弄清楚如何触发每个圆圈的鼠标悬停事件以填充表格。
from lxml import etree
from xml.etree import ElementTree
from selenium import webdriver
driver_path = "#Firefox web driver"
browser = webdriver.Firefox(executable_path=driver_path)
browser.get("https://slushpool.com/stats/?c=btc")
page = browser.page_source #Get page html
tree = etree.HTML(page) #create etree
table_Xpath = '/html/body/div[1]/div/div/div/div/div[5]/div[1]/div/div/div[2]/div[2]/div[2]/div/table'
table =tree.xpath(table_Xpath) #get table using Xpath
print(ElementTree.tostring(table[0])) #Returns empty table.
#Should return data from each mouseover event
Run Code Online (Sandbox Code Playgroud)
有没有办法触发每个圆圈的鼠标悬停事件,然后提取生成的数据。
预先感谢您的帮助!
要触发每个圆圈的鼠标悬停事件,您必须引发WebDriverWait ,并且visibility_of_all_elements_located()可以使用以下定位器策略:
代码块:
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.webdriver.common.action_chains import ActionChains
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://slushpool.com/stats/?c=btc")
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1//span[text()='Distribution']"))))
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//h1//span[text()='Distribution']//following::div[1]/*[name()='svg']//*[name()='g']//*[name()='g' and @class='paper']//*[name()='circle']")))
for element in elements:
ActionChains(driver).move_to_element(element).perform()
Run Code Online (Sandbox Code Playgroud)浏览器快照:
| 归档时间: |
|
| 查看次数: |
3850 次 |
| 最近记录: |