Search code examples
pythonhtmlseleniumxpathwebautomation

How to click element that's contained within multiple html tags using selenium in python


I'm trying to develop a website automation using selenium webdriver on python, but i'm having trouble clicking on a certain tag.

The website I'm trying to automate is very old and contains multiple iframe and <'html'> tags, and that's giving me a hard time on finding the xpath of the element want to click.

This is the resultant xpath using the copy xpath feature of browser:

 /html/body/div/table[2]/tbody/tr/td/table/tbody/tr[1]/td[1]/table/tbody/tr/td/table/tbody/tr/td/div/span[2]/div[1]/img

However, it appears that this xpath is only considering the last <'html'> tag the element is contained, and not all of them, and therefore it's not being able to execute the click.

I tried changing the index of the <'html'> tag, for example

 html[2]/body/...

but it didn't work.

Any ideas?


Solution

  • You haven't shown us any of your code, but I bet that it fails to switch to the target frame. With iframes, you must instruct the webdriver to switch_to to each iframe before it will find elements within the frame. If you update your question with some of your code, I can show you where the switch_to statements might go.