简体   繁体   English

无法使用 xpath 和 selenium Python 访问特定元素

[英]Can't access specific element using xpath with selenium Python

I am trying to parse the wind direction using selenium and I think using xpath is the easiest way to get this info.我正在尝试使用 selenium 解析风向,我认为使用 xpath 是获取此信息的最简单方法。 There is a table with all the information and the xpath of the elements within this table follow the same structure, hence my following code:有一个包含所有信息的表格,该表格中元素的 xpath 遵循相同的结构,因此我的代码如下:

wind_directions = [browser.find_element_by_xpath(f'//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[{i}]').text for i in range(14,25)]

Indeed, the structure of the data on the site is the following:事实上,网站上的数据结构如下: 表中的数据结构

My issue is that I would like to get the content "rotate(494, 50, 50) translate(0,5)" from the picture above but I can't: If I try to write replace the previous fstring with f'//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[{i}]/svg/g'] , The compiler tells me that Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[14]/svg/g"} .我的问题是,我想从上图中获取内容“rotate(494, 50, 50) translate(0,5)”,但我做不到:如果我尝试写入,请将之前的 fstring 替换为f'//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[{i}]/svg/g'] ,编译器告诉我Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[14]/svg/g"} .

Any idea why I get such a message while this is the exact xpath that appears when I check on the element on Chrome?知道为什么我会收到这样的消息,而这是我在 Chrome 上检查元素时出现的确切 xpath 吗? (I triple-checked the indexes in the fstring and it is not the source of the error). (我对 fstring 中的索引进行了三重检查,它不是错误的来源)。

svg g etc. are special tag names. svg g等是特殊标签名称。
To locate such nodes with XPath you can change your XPath expression as following:要使用 XPath 定位此类节点,您可以更改 XPath 表达式,如下所示:

'//*[@id="archive_results"]/table/tbody/tr/td/table/tbody/tr[3]/td[{i}]/*[name()="svg"]/*[name()="g"]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM