【问题标题】:Scraping issue w/ Python & Selenium (Unable to locate element)使用 Python 和 Selenium 进行抓取问题(无法定位元素)
【发布时间】:2017-06-20 09:51:35
【问题描述】:

我一直在尝试抓取关注网站 (http://epl.squawka.com/english-premier-league/21-05-2017/watford-vs-man-city/matches)。我想要做的是单击“镜头”,然后让 javascript 加载,然后将鼠标悬停在每个“镜头元素”上并提取出现的数据(时间和玩家信息)。

我的问题是,当我尝试将鼠标悬停在“拍摄元素”上时,我不断收到“无法定位元素”错误。当我在尝试执行此操作时打印 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()

【问题讨论】:

    标签: javascript python selenium selenium-webdriver


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多