使用 selenium python 点击​​ svg [英] Clicking on svg using selenium python

查看:92
本文介绍了使用 selenium python 点击​​ svg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

svg 图标是可点击的.

<svg aria-label="Search" class="some-icon" width="24" height="24" fill="#000" viewBox="0 0 24 24"><path d="M9.5,...,5 9.5,5Z"></路径></svg>

示例代码:

from selenium import webdriver驱动程序 = webdriver.Chrome(CHROME_DRIVER_LOCATION)driver.find_element_by_xpath('//*[@id="SearchForm"]/div[1]/span/div[1]/div[2]/svg/path').click()

错误:

<块引用>

没有这样的元素:无法定位元素:{"method":"xpath","selector":"//*[@id="SearchForm"]/div[1]/span/div[1]/div[2]/svg/path"}

解决方案

click() 在 svg 图标上,您可以使用以下解决方案:

driver.find_element_by_xpath('//div[@class="some-class"]/*[name()="svg"][@aria-label="Search"]').click()

您可以在以下位置找到一些相关讨论:

svg icon is clickable.

<div class="some-class">
    <svg aria-label="Search" class="some-icon" width="24" height="24" fill="#000" viewBox="0 0 24 24">
        <path d="M9.5,...,5 9.5,5Z">
        </path>
    </svg>
</div>

Sample code:

from selenium import webdriver

driver = webdriver.Chrome(CHROME_DRIVER_LOCATION)
driver.find_element_by_xpath('//*[@id="SearchForm"]/div[1]/span/div[1]/div[2]/svg/path').click()

Error:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="SearchForm"]/div[1]/span/div[1]/div[2]/svg/path"}

解决方案

To click() on the svg icon you can use the following solution:

driver.find_element_by_xpath('//div[@class="some-class"]/*[name()="svg"][@aria-label="Search"]').click()

You can find a couple of relevant discussions in:

这篇关于使用 selenium python 点击​​ svg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆