如何使用 Selenium 和 Python 识别嵌套元素
How to identify the nested element using Selenium and Python
我可能在 <svg>
中有一个我似乎无法访问的嵌套元素
我试过使用
driver.find_element(By.CSS_SELECTOR, 'button.login-fake-btn')
和
driver.find_element(By.CSS_SELECTOR, 'login-fake-btn')
和其他一些人。
HTML嵌套svg的结构:
<svg class="1">
<div id="2">
<div>
<div class="3">
<div class="4">
<li>
<button class="5" type="button" id="login-fake-btn">
...closing tags
HTML 的快照:
我用 xpath 也没有成功。
错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.login-fake-btn"}
如何使用 css 选择器(或 xpath,但我理解 css 更好)获得嵌套的 svg?
它是一个 <button>
元素,它在 <svg>
标签之外,并且您可能会在其上调用 click()
。因此,要找到您必须归纳的元素 for the element_to_be_clickable()
and you can use either of the following :
使用CSS_SELECTOR
:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']")))
使用XPATH
:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='login-btn btn-shadow' and @id='login-fake-btn'][@data-testid='login-fake-btn']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 的一些相关讨论:
我可能在 <svg>
中有一个我似乎无法访问的嵌套元素
我试过使用
driver.find_element(By.CSS_SELECTOR, 'button.login-fake-btn')
和
driver.find_element(By.CSS_SELECTOR, 'login-fake-btn')
和其他一些人。
HTML嵌套svg的结构:
<svg class="1">
<div id="2">
<div>
<div class="3">
<div class="4">
<li>
<button class="5" type="button" id="login-fake-btn">
...closing tags
HTML 的快照:
我用 xpath 也没有成功。
错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.login-fake-btn"}
如何使用 css 选择器(或 xpath,但我理解 css 更好)获得嵌套的 svg?
它是一个 <button>
元素,它在 <svg>
标签之外,并且您可能会在其上调用 click()
。因此,要找到您必须归纳的元素 element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']")))
使用
XPATH
:element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='login-btn btn-shadow' and @id='login-fake-btn'][@data-testid='login-fake-btn']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于