Home > Enterprise >  Can't access specific element using xpath with selenium Python
Can't access specific element using xpath with selenium Python

Time:11-17

I am trying to parse the wind direction using selenium and I think using xpath is the easiest way to get this info. 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:

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: Data structure in the table

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"}.

Any idea why I get such a message while this is the exact xpath that appears when I check on the element on Chrome ? (I triple-checked the indexes in the fstring and it is not the source of the error).

CodePudding user response:

svg g etc. are special tag names.
To locate such nodes with XPath you can change your XPath expression as following:

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