如何使用 Selenium 和 Python 单击 svg 标签内的下拉按钮

编程入门 行业动态 更新时间:2024-10-25 18:34:40
本文介绍了如何使用 Selenium 和 Python 单击 svg 标签内的下拉按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个网站,我想在其中下载 Excel 文件.:

  • 如何访问 '通过 Selenium-Python 的 rect' 类型元素
  • 使用 selenium python 点击​​ svg

I have a website where i want to download an excel file. www.rivm.nl/media/smap/eenzaamheid.html

I want to be able to click the download button and then click download xls. To click the first button i tried the following:

WebDriverWait(driver, 10).until(EC.visibility_by_element_located(By.XPATH('//path[@d="M 11.6 5 L 11.6 17 M 7.4 12.6 L 11.6 17 L 15.799999999999999 12.6 M 3 19 L 3 21 L 21 21 L 21 19"]'))).click()

and

WebDriverWait(driver, 10).until(EC.visibility_by_element_located(By.XPATH('//path[@stroke-width="1.8"]'))).click() WebDriverWait(driver, 10).until(EC.visibility_of_element_located(By.XPATH('//g[@aria-label="View export menu"]'))).click() WebDriverWait(driver, 10).until(EC.visibility_of_element_located(By.XPATH('//g[@aria-label="Chart export menu"]'))).click()

For the last two i get the " 'str' object is not callable" error, the first two options just don't work at all.

I can't seem to be able to click the button. I can't really find an 'id' or an unique class/name (the print button has the same class value) for the button, which would make this a lot easier.

I think i would also not be able to then click the "XLS downloaden" as I can't find an ID here either.

So, how would i go about clicking the first button, and then clicking the "XLS downloaden" ?

解决方案

The desired element is a svg element so to click on the element instead of visibility_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("www.rivm.nl/media/smap/eenzaamheid.html") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.highcharts-container svg g[aria-label='View export menu'] rect"))).click()

  • Using XPATH:

    driver.get("www.rivm.nl/media/smap/eenzaamheid.html") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='highcharts-container ']//*[name()='svg']//*[name()='g' and @aria-label='View export menu']//*[name()='rect']"))).click()

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait from selenium.webdrivermon.by import By from selenium.webdriver.support import expected_conditions as EC

  • Browser Snapshot:


References

You can find a couple of relevant discussions on interacting with SVG element in:

  • How to access to 'rect' type element through Selenium-Python
  • Clicking on svg using selenium python

更多推荐

如何使用 Selenium 和 Python 单击 svg 标签内的下拉按钮

本文发布于:2023-11-27 15:51:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   单击   按钮   标签   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!