0

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.

HTML tree

vvvvv
  • 25,404
  • 19
  • 49
  • 81
golidar
  • 1
  • 1
  • Hi @golidar How about the issue? Is [my answer below](https://stackoverflow.com/questions/70314928/how-to-address-a-hyperlink-in-a-nested-html-in-selenium-python-by-ie11/70331028#70331028) helpful to deal with the issue? Please let me know if there is anything that I can help here. – Yu Zhou Dec 15 '21 at 07:15

1 Answers1

0

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();
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22