2017-06-20 69 views
0

我一直在试图抓取下面的网站(http://epl.squawka.com/english-premier-league/21-05-2017/watford-vs-man-city/matches)。我想要做的是点击“镜头”,然后让JavaScript负载,然后将鼠标悬停在每个“镜头元素”上,然后拖动出现的数据(时间和播放器信息)。Scrapping issue w/Python&Selenium(Unable to locate element)

我的问题是,当试图将鼠标悬停在“拍摄元素”上时,我一直在获取“无法定位元素”错误。当我在这一点上打印page_source的时候,我可以清楚地看到其中的元素,以及当我保存屏幕截图时可见的元素。

我认为元素可能会以某种方式隐藏。我试着'切换到'(也许不正确),无济于事。

driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver') 
driver.get("http://epl.squawka.com/english-premier-league/21-05-2017/watford-vs-man-city/matches") 

try: 
    element = WebDriverWait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='mc-content-wrap']/div[2]/div[1]"))) 
finally: 
    driver.find_element_by_xpath("//*[@id='mc-content-wrap']/div[2]/div[1]").click() 

time.sleep(1) 
driver.find_element_by_id("mc-stat-shot").click() 
time.sleep(5) 
shotsVar = -1 
html = driver.page_source 
bsObj = BeautifulSoup(html, "html.parser") 
for circle in bsObj.find("svg",{'height':'224.6'}).findAll('circle'): 
    shotsVar += 1 
    if circle['r'] == '6.5': 
     shotsXpathCode = ("//*[@id='mc-pitch-view']/div[2]/div[1]/svg/g[%s]/circle" % shotsVar) 
     print(shotsXpathCode) 
     try: 
      element = WebDriverWait(driver, 100).until(EC.presence_of_element_located((By.XPATH,"%s" % shotsXpathCode))) 
     finally: 
      element_to_hover_over = driver.find_element_by_xpath("%s" % shotsXpathCode) 
      hover = ActionChains(driver).move_to_element(element_to_hover_over) 
      hover.perform() 

回答

0

Xpath和SVG似乎不能很好地协同工作。

尝试以下XPath:

"//*[@id='mc-pitch-view']/div[2]/div[1]/*[name()='svg']/*[name()='g'][%s]/*[name()='circle']" 

相关:Find the nth child under svg using xpath

+0

惊人!这工作!谢谢! –

相关问题