如何使用Selenium Python单击Canvas中的Element

时间:2015-07-26 10:05:11

标签: python selenium canvas selenium-webdriver selenium-ide

我正在尝试点击第一个canvas元素,但它没有点击任何内容。我尝试了以下方法:

第一种方式

menu = driver.find_element_by_xpath("*//div[@id = 'cy']/div/canvas")
ActionChains(driver).move_to_element(menu).perform()

第二种方式

driver.find_element_by_xpath("*//div[@id = 'cy']/div/canvas").click()

这是html代码:

<div id="cy">
<div style="position: absolute; z-index: 0; overflow: hidden; width: 1029px; height: 130px;">
<canvas style="position: absolute; z-index: 5; width: 1029px; height: 130px;" data-id="layer0-selectbox" width="1029" height="130"></canvas>
<canvas style="position: absolute; z-index: 4; width: 1029px; height: 130px;" data-id="layer1" width="1029" height="130"></canvas>
<canvas style="position: absolute; z-index: 3; width: 1029px; height: 130px;" data-id="layer2-drag" width="1029" height="130"></canvas>
<canvas style="position: absolute; z-index: 2; width: 1029px; height: 130px;" data-id="layer3" width="1029" height="130"></canvas>
<canvas style="position: absolute; z-index: 1; width: 1029px; height: 130px;" data-id="layer4-node" width="1029" height="130"></canvas>
</div>
</div>