使用 Selenium 单击 SVG 时出错 Python
Error in Clicking on SVG using Selenium Python
以下是 HTML 代码。我想点击“导出为 CSV”。
<pre>
<div id="leo-title-bar" style="width: 100%">
<div class="container-fluid p-0"><div class="no-gutters" style="min-height: 100vh;">
<div class="col-12 d-flex flex-column">
<nav class="navbar navbar-dark bg-primary justify-content-start" style="height: 64px; flex-wrap: unset;">
<span class="navbar-brand" style="flex: 1 1 0%; font-size: 22px;">Agency Summary</span>
<svg aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8" data-prefix="fas" data-icon="download" class="svg-inline--fa fa-download fa-w-16 svg-shadow svg-icon-basic svg-icon-basic-hover" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<title id="svg-inline--fa-title-5AhAR2Z9sKF8">Export to CSV</title>
<path fill="currentColor" d="M216 0h80c13....."></path></svg>
</div></main>
</div>
</div>
</div>
</div>
</pre>
我试过以下代码:
from selenium import webdriver
driver = webdriver.Edge(PATH)
driver.find_element_by_xpath('//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
出现错误:
selenium.common.exceptions.NoSuchElementException
可能您错过了延迟。
所以添加一些像
这样的虚拟睡眠
from selenium import webdriver
import time
driver = webdriver.Edge(PATH)
time.sleep(5)
driver.find_element_by_xpath('//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
应该可以解决您的问题。
你的定位器看起来也很糟糕。您必须创建更可靠的定位器。
您还应该使用预期条件显式等待,如下所示:
from selenium import webdriver
import time
rom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(PATH)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]'))).click()
改变
.../*[name()="svg"]...
至
.../*[local-name()="svg"]...
在您的 XPath-expression 中,因为您的 <svg...>
元素位于命名空间 xmlns="http://www.w3.org/2000/svg"
中。事情是 name()
匹配 namespace:name,但 local-name()
只匹配没有名称空间(-prefix)的名称。
所需的元素是 svg element. To on the element you have to induce for the and you can use either of the following :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#leo-title-bar svg[data-icon='download']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='leo-title-bar']//*[name()='svg' and @data-icon='download']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 的一些相关讨论:
以下是 HTML 代码。我想点击“导出为 CSV”。
<pre>
<div id="leo-title-bar" style="width: 100%">
<div class="container-fluid p-0"><div class="no-gutters" style="min-height: 100vh;">
<div class="col-12 d-flex flex-column">
<nav class="navbar navbar-dark bg-primary justify-content-start" style="height: 64px; flex-wrap: unset;">
<span class="navbar-brand" style="flex: 1 1 0%; font-size: 22px;">Agency Summary</span>
<svg aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8" data-prefix="fas" data-icon="download" class="svg-inline--fa fa-download fa-w-16 svg-shadow svg-icon-basic svg-icon-basic-hover" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<title id="svg-inline--fa-title-5AhAR2Z9sKF8">Export to CSV</title>
<path fill="currentColor" d="M216 0h80c13....."></path></svg>
</div></main>
</div>
</div>
</div>
</div>
</pre>
我试过以下代码:
from selenium import webdriver
driver = webdriver.Edge(PATH)
driver.find_element_by_xpath('//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
出现错误:
selenium.common.exceptions.NoSuchElementException
可能您错过了延迟。
所以添加一些像
from selenium import webdriver
import time
driver = webdriver.Edge(PATH)
time.sleep(5)
driver.find_element_by_xpath('//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
应该可以解决您的问题。
你的定位器看起来也很糟糕。您必须创建更可靠的定位器。
您还应该使用预期条件显式等待,如下所示:
from selenium import webdriver
import time
rom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(PATH)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="col-12 d-flex flex-column"]/*[name()="svg"][@aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]'))).click()
改变
.../*[name()="svg"]...
至
.../*[local-name()="svg"]...
在您的 XPath-expression 中,因为您的 <svg...>
元素位于命名空间 xmlns="http://www.w3.org/2000/svg"
中。事情是 name()
匹配 namespace:name,但 local-name()
只匹配没有名称空间(-prefix)的名称。
所需的元素是 svg element. To
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#leo-title-bar svg[data-icon='download']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='leo-title-bar']//*[name()='svg' and @data-icon='download']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于