0

I have been trying to click an accept button in an iframe, but I am unable to locate this iframe with xpath. I have checked similar SO posts but to no avail.

Selenium can't locate iframe, python selenium cant find iframe xpath

This is the HTML of the page.

I have tried following so far.

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pop-frame028142186741537767"))) gives TimeoutException.

Also tried:

time.sleep(10) driver.switch_to.frame(driver.find_element_by_xpath("//div[@class='truste_box_overlay']/div[@class='truste_box_overlay_inner']/iframe[@id='pop-frame028142186741537767']"))

Error

NoSuchElementException: Unable to locate element: //div[@class='truste_box_overlay']/div[@class='truste_box_overlay_inner']/iframe[@id='pop-frame028142186741537767']

Finding the the iframe driver.find_elements_by_id('pop-frame028142186741537767') gives an empty list. However, I am able to locate two parent div elements with xpath and class attributes.

EDIT: driver.find_elements_by_class_name('truste_box_overlay') gives outermost div element. But, accessing same element with id driver.find_elements_by_id('pop-div20738421000570183') gives empty list.

So, I suppose find_elements_by_id not working?! How can I go about to solve this problem? I am not able to progress as without clicking this button I can't interact to the main content.

prp
  • 3
  • 2
  • If you have just 1 iframe on the site, then you can locate it by tag name. – Ratmir Asanov Nov 18 '19 at 12:41
  • @RatmirAsanov I am afraid locating by tag name will not work. There are a number of iframes. – prp Nov 18 '19 at 13:09
  • Is this "pop-frame028142186741537767" static? If you're trying to find by ID and the ID is not static that might be your problem. You can find by XPath using partial search //iframe[contains(@id, 'pop-frame')] – ratsstack Nov 19 '19 at 01:19
  • 1
    @ratsstack The partial search actually solves the problem. I was stupidly inspecting the same page opened first time for hours and did not check subsequent instances in experiments. It never crossed my mind that ID could be dynamic in this case. Thank you for the solution! – prp Nov 19 '19 at 11:04
  • Great to hear. I have posted this as an answer. Would appreciate if you would accept it so I can build some reputation :-) – ratsstack Nov 20 '19 at 23:17

2 Answers2

0

Switch back to the default content (out of the ):

driver.switch_to.default_content()

driver.switch_to.frame("iframe name")

driver.find_elements_by_class_name('truste_box_overlay')

Hope this will fix your issue.

stacktome
  • 790
  • 2
  • 9
  • 32
  • I am able to find `div` element with `truste_box_overlay` as class name. But, as I mentioned, I am unable to locate `iframe` by id (there is no `name` attribute). I get `NoSuchFrameException: pop-frame028142186741537767`. I didn't get the purpose of `driver.switch_to.default_content()` at the start when we are already interacting with main page. Still, I tried it, but it doesn't work. – prp Nov 18 '19 at 16:50
0

You are searching by an ID which is auto-generated and will not work the next time the page is reloaded. Instead try XPath using partial search //iframe[contains(@id, 'pop-frame')]

ratsstack
  • 1,012
  • 4
  • 13
  • 32