如何使用Selenium和Python单击<svg:image>元素

时间:2019-04-03 09:39:22

标签: python selenium svg xpath webdriverwait

在下面提供了xpath:

<svg:image xlink:href="some.svg" class="holder-38" width="24" height="268" preserveAspectRatio="none" x="426.7" y="473" type="image/svg+xml" data-ember-action="" data-ember-action-12238="12238">

我可以使用xpath(没有标记为'*')访问此文件:

'//*[@class="holder-38"]'

但无法使用标记为svg:image的访问权限:

'//svg:image[@class="holder-38"]'

如何在此处指定标签?

2 个答案:

答案 0 :(得分:1)

<svg:image>

<svg:image>元素包括SVG文档中的图像。它可以显示raster image个文件或其他SVG文件。 SVG软件必须支持的唯一图像格式是JPEG,PNG和其他SVG文件。动画GIF行为未定义。

<image>显示的

SVG文件是treated as an image,其中没有加载外部资源,:visited的样式为aren't applied,它们不能交互。要包含动态SVG元素,请尝试将<use>与外部URL一起使用。要包含SVG文件并在其中运行脚本,请在<object>内尝试<foreignObject>

  

注意:HTML规范在解析HTML时将<image>定义为<img>的同义词。此特定元素及其行为仅适用于SVG文档或inline SVG


xlink:href

xlink:href属性将到资源的链接定义为引用<IRI>。该链接的确切含义取决于使用它的每个元素的上下文。

  

从SVG 2开始不推荐使用:不再推荐使用此功能。尽管一些浏览器可能仍然支持它,它可能已经从相关网页标准去除,可能是被丢弃的过程中,或只可保持兼容性的目的。避免使用它,并尽可能更新现有代码;看到兼容表在这个页面的底部来指导你的决定。请注意,这个功能可能会停止工作在任何时候。

     

注意:SVG 2消除了对xlink名称空间的需要,因此应使用href代替xlink:href


解决方案

要对所需元素进行click(),您需要为所需element_to_be_clickable引入 WebDriverWait ,并且可以使用以下解决方案:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg:image' and starts-with(@class, 'holder') and contains(@xlink:href, 'some')]"))).click()

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

答案 1 :(得分:0)

尝试以下方式访问tag_name。

'//*[local-name()="svg:image"][@class="holder-38"]'

要使用Action类单击元素。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
elememnt=WebDriverWait(driver, 10).until(ec.presence_of_element_located(("xpath", '//*[local-name()="svg:image"][@class="holder-38"]')))
ActionChains(driver).move_to_element(elememnt).click().perform()