How to click on a <svg:image> element using

2019-07-29 13:09发布

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?

2条回答
Animai°情兽
2楼-- · 2019-07-29 13:39

Try the following way to access tag_name.

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

To click on the element use 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()
查看更多
戒情不戒烟
3楼-- · 2019-07-29 13:42

<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 of xlink:href you should use href.


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
查看更多
登录 后发表回答