[英]How to click on a path element within g tag using Python Selenium
I am trying to click path element with selenium python.我正在尝试使用 selenium python 单击路径元素。
Code trials:代码试验:
def find_the_place_path(self):
# firstly I will get the all path tags
all_place_in_path = self.driver.find_elements_by_tag_name("path")
# title of my path tag is "gat: 3"
for place in all_place_in_path:
print(place.get_attribute('title'))
if place.get_attribute('title') == "gat: 3":
# this code id not worked for me
place.click()
HTML snapshot: HTML 快照:
path is a svg element.路径是 svg 元素。 use the following
xpath
to identify the element first and then click.使用下面的
xpath
先识别元素然后点击。
XPATH: //*[name()='path'][@title='gat: 3']
To click on dynamic element use WebDriverWait()
and wait for element_to_be_clickable()
要单击动态元素,请使用
WebDriverWait()
并等待element_to_be_clickable()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='path'][@title='gat: 3']"))).click()
You need to import following libraries您需要导入以下库
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
To click on the <path>
element as it is within SVG namespace you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要点击SVG命名空间中的
<path>
元素,您需要为element_to_be_clickable()引入WebDriverWait ,并且可以使用以下任一定位器策略:
Using CSS_SELECTOR :使用CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg g.coupe path[fill-rule='nonzero']"))).click()
Using XPATH :使用XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg']//*[name()='g' and @class='coupe']//*[name()='path' and @fill-rule='nonzero']"))).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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.