如何使用 Selenium 和 Python 单击 <svg:image> 元素
How to click on a <svg:image> element using Selenium and Python
提供了以下 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"]'
如何在此处指定标签?
尝试以下方式访问tag_name.
'//*[local-name()="svg:image"][@class="holder-38"]'
点击元素使用 Action
Class.
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()
<svg:image>
<svg:image>
element includes images inside SVG documents. It can display raster image 文件或其他 SVG 文件。 SVG 软件必须支持的唯一图像格式是 JPEG、PNG 和其他 SVG 文件。动画 GIF 行为未定义。
用 <image>
显示的 SVG 文件是 treated as an image where external resources aren't loaded, :visited styles aren't applied and they cannot be interactive. To include dynamic SVG elements, try <use>
with an external URL. To include SVG files and run scripts inside them, try <object>
inside of <foreignObject>
.
Note: The HTML spec defines <image>
as a synonym for <img>
while parsing HTML. This specific element and its behavior only apply inside SVG documents or inline SVG.
xlink:href
xlink:href attribute defines a link to a resource as a reference <IRI>
。 link 的确切含义取决于使用它的每个元素的上下文。
Deprecated since SVG 2: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Note: SVG 2 removed the need for the xlink
namespace, so instead of xlink:href
you should use href
.
解决方案
To 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
提供了以下 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"]'
如何在此处指定标签?
尝试以下方式访问tag_name.
'//*[local-name()="svg:image"][@class="holder-38"]'
点击元素使用 Action
Class.
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()
<svg:image>
<svg:image>
element includes images inside SVG documents. It can display raster image 文件或其他 SVG 文件。 SVG 软件必须支持的唯一图像格式是 JPEG、PNG 和其他 SVG 文件。动画 GIF 行为未定义。
用 <image>
显示的 SVG 文件是 treated as an image where external resources aren't loaded, :visited styles aren't applied and they cannot be interactive. To include dynamic SVG elements, try <use>
with an external URL. To include SVG files and run scripts inside them, try <object>
inside of <foreignObject>
.
Note: The HTML spec defines
<image>
as a synonym for<img>
while parsing HTML. This specific element and its behavior only apply inside SVG documents or inline SVG.
xlink:href
xlink:href attribute defines a link to a resource as a reference <IRI>
。 link 的确切含义取决于使用它的每个元素的上下文。
Deprecated since SVG 2: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Note: SVG 2 removed the need for the
xlink
namespace, so instead ofxlink:href
you should usehref
.
解决方案
To 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