如何单击 <svg:image>使用 Selenium 和 Python 的元素 [英] 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话
我可以使用 xpath 访问它(没有标记为*"):
'//*[@class="holder-38"]'
但无法使用标签访问 svg:image
:
'//svg:image[@class="holder-38"]'
如何在此处指定标签?
解析 HTML.此特定元素及其行为仅适用于 SVG 文档或 内联 SVG.
xlink:href
xlink:href 属性将指向资源的链接定义为参考 <IRI>
.该链接的确切含义取决于使用它的每个元素的上下文.
自 SVG 2 起已弃用:不再推荐此功能.虽然一些浏览器可能仍然支持它,但它可能已经从相关的 Web 标准中删除,可能正在被删除,或者可能只是为了兼容性目的而保留.避免使用它,并尽可能更新现有代码;请参阅本页底部的兼容性表以指导您的决定.请注意,此功能可能随时停止工作.
注意:SVG 2 不再需要 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') 和 contains(@xlink:href, 'some')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import By从 selenium.webdriver.support 导入 expected_conditions 作为 EC
Provided an xpath below:
<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">
I am able to access this with xpath (without tag as '*'):
'//*[@class="holder-38"]'
But unable to access with tag as svg:image
:
'//svg:image[@class="holder-38"]'
How can I specify the tag here?
<svg:image>
The <svg:image>
element includes images inside SVG documents. It can display raster image files or other SVG files. The only image formats SVG software must support are JPEG, PNG, and other SVG files. Animated GIF behavior is undefined.
SVG files displayed with <image>
are 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
The xlink:href attribute defines a link to a resource as a reference <IRI>
. The exact meaning of that link depends on the context of each element using it.
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
.
Solution
To click()
on the desired element you need to induce WebDriverWait for the desired element_to_be_clickable
and you can use the following solution:
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()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于如何单击 <svg:image>使用 Selenium 和 Python 的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!