使用Python和Selenium将图像悬停在图上 [英] Scraping hover over figure using Python and Selenium

查看:251
本文介绍了使用Python和Selenium将图像悬停在图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 http://fuelinsights.gasbuddy.com/Charts 中抓取数据使用Python和Selenium。困难的部分是数据只在线图上的一个点悬停时出现。目前,我的问题是无法创建所有悬停在对象上的列表。我的代码到目前为止:

  from selenium import webdriver as web $ b $ from selenium.webdriver.common.action_chains import ActionChains 

driver = web.Chrome('driver path')

driver.get('http://fuelinsights.gasbuddy.com/Charts')

test = driver.find_elements_by_xpath('// * [@ class =highcharts-markers]')

print(test)

`



这给了我test = []。之前,我已经用我的所有抓取项目的美丽,但我已经重做了一些我以前的项目,以确保我了解硒是如何工作,没有问题。

如果任何人都可以帮我解决这个问题,那么我可以创建一个项目列表,我可以使用ActionChains将鼠标悬停并从中提取价格和日期非常感谢。



谢谢!



****编辑****
至澄清,我查看了其他许多关于SVG和g元素和Highcharts的帖子,但我仍然很难解决这个问题。我已经尝试了许多Xpaths(和其他find_elements_by选项),但只能得到两个结果:(1)Xpath有效,但不包含任何元素,或(2)InvalidSelectorException指示我无法定位一个带有xpath表达式的元素。我相信这只是错误地指定了我的Xpath,但我不知道如何找到正确的Xpath。 解决方案

您不能使用上面提到的Xpath来定位svg标签中的元素。



Xpath您可以使用它来创建列表hover objects is:


$ b

// [name()='svg'] // [name()=' g'和@ class ='highcharts-markers'] / * [name()='path']



获取所有工具提示元素的文本。您可以使用逻辑并编写相应的Python代码:

1。 获取工具提示元素列表

  List< WebElement> highChartElements = driver.findElements(By.xpath(// * [name()='svg'] // * [name()='g'and @ class ='highcharts-markers'] / * [name() = '路径'])); 

2。遍历列表并使用动作类来移动并点击所有工具提示元素

3。获取工具提示元素的文本。

  for(WebElement element:highChartElements){
Actions action =新的动作(驱动程序);
action.moveToElement(element).click()。perform();
Thread.sleep(3000);
列表< WebElement> highChartToolTipTextElements = driver.findElements(By.xpath(// * [name()='svg'] // * [name()='g'and @ class ='highcharts-tooltip'] / * [name() = '文本'] / * [名称()= 'TSPAN']));
for(WebElement toolTipElement:highChartToolTipTextElements){
System.out.println(元素的文本是+ toolTipElement.getText());
}
}


I am trying to scrape the data from http://fuelinsights.gasbuddy.com/Charts using Python and Selenium. The difficult part is that the data only appear when a point on the line graph is hovered over. Currently, my issue is an inability to create a list of all the hover over objects. My code so far is below:

from selenium import webdriver as web
from selenium.webdriver.common.action_chains import ActionChains

driver = web.Chrome('driver path')

driver.get('http://fuelinsights.gasbuddy.com/Charts')

test= driver.find_elements_by_xpath('//*[@class="highcharts-markers"]')

print(test)

`

which gives me test=[]. Previously, I have used beautifulsoup for all of my scraping projects, but I have redone some of my previous projects to make sure that I understand how Selenium works and haven't had issues.

If anyone can help me solve this issue so I can create a list of the items that I can use ActionChains to hover over and extract the price and date from it would be much appreciated.

Thank you!

****EDIT**** To clarify, I have looked over numerous other posts concerning SVG and g elements and Highcharts, but I am still short on a solution to this problem. I have tried numerous Xpaths (and other find_elements_by options), but have only been able to come to two results: (1) the Xpath is valid, but does not contain any elements, or (2) InvalidSelectorException indicating that I was unable to locate an element with the xpath expression. I believe this comes down to simply incorrectly specifying my Xpath, but I am at a loss for how to find the correct Xpath.

解决方案

You can't use the Xpath which you have mentioned above for locating the elements inside the svg tag.

Xpath which you can use to create a list of hover objects is:

"//[name()='svg']//[name()='g' and @class='highcharts-markers']/*[name()='path']"

I have written a java program for getting the text of all the tool-tip elements. You can use the logic and write a corresponding python code:

1. Get List of tooltip Elements

 List <WebElement> highChartElements= driver.findElements(By.xpath("//*[name()='svg']//*[name()='g' and @class='highcharts-markers']/*[name()='path']"));

2. Iterate through the list and use action class for moving and clicking on all the tooltip Elements

3. Get the text of the tooltip elements.

for(WebElement element:highChartElements){
        Actions action = new Actions(driver);
        action.moveToElement(element).click().perform();
        Thread.sleep(3000);
        List<WebElement> highChartToolTipTextElements= driver.findElements(By.xpath("//*[name()='svg']//*[name()='g' and @class='highcharts-tooltip']/*[name()='text']/*[name()='tspan']"));
        for(WebElement toolTipElement:highChartToolTipTextElements){
            System.out.println("The text for the elements is"+toolTipElement.getText());
        }
    }

这篇关于使用Python和Selenium将图像悬停在图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆