'How to address a hyperlink in a nested HTML in selenium python by IE11?
Could everyone help me solving the problem to click on a hyperlink in a HTML page that itself contains HTML element inside it's body( 4 level nested HTMLs)? I have tried different ways to address XPATH, but they couldn't being known during run. I use selenium, Python3.9, IE11( for this project is mandatory to use IE11).
Its full Xpath from the main HTML is:
/html/body/div[2]/div[2]/iframe/html/frameset/frame[2]/html/body/div/div[4]/div/iframe/html/body/div/div[@id='resultList']/div[2]/div[3]/table/tbody/tr/td[@id='resultList_0_3']/a
and I used this python code to access it:
web_driver.find_element_by_xpath("/html/body/div[2]/div[2]/iframe/html/frameset/frame[2]/html/body/div/div[4]/div/iframe/html/body/div/div[@id='resultList']/div[2]/div[3]/table/tbody/tr/td[@id='resultList_0_3']/a").click()
the output error is:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == /html/body/div2/div2/iframe/html/frameset/frame2/html/body/div/div[4]/div/iframe/html/body/div/div[@id='resultList']/div2/div[3]/table/tbody/tr/td[@id='resultList_0_3']/a
and the same error for other elements searched by id, tag,...
B.S. when I try to access Xpath: /html/body/div[2]/div[2]/iframe the selenium found the link successfully, but did not find after that level.

Solution 1:[1]
The point is that you need to switch to the iframe first, then you can locate the elements in the iframe. You can refer to this blog and this thread about how to switch to nested iframes in selenium.
First, you need to find the outside iframes and store them in web elements, then you can switch to the iframes. If the iframes are nested, you need to switch to them one by one.
I assume that you can find the iframes by ids, the sample code is like below (You need to change the url, path and ids to your owns. You can also find the iframes by other ways, like xpath):
from selenium import webdriver
from selenium.webdriver.common.by import By
url = "https://your_site.com"
driver = webdriver.Ie('your_path\\IEDriverServer.exe')
driver.get(url)
frame1 = driver.find_element(By.ID, id1);
driver.switch_to.frame(frame1);
frame2 = driver.find_element(By.ID, id2);
driver.switch_to.frame(frame2);
frame3 = driver.find_element(By.ID, id3);
driver.switch_to.frame(frame3);
driver.find_element(By.ID, id_of_hypelink).click();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yu Zhou |
