Xpath / xclick: How to traverse through an iframe

I want to find and click a button in an iframe using a xpath locator.

The (not working) full xpath is: /html/body/div[@id='sp_message_container_533914']/iframe/html/body/div/div[2]/div[3]/div[2]/button[@class='sp_choice_type_11']

UI Vision finds /html/body/div[@id='sp_message_container_533914']/iframe[@id='sp_message_iframe_533914'], but I can’t traverse into the iframe.

I found two stackexchange hints about this:

1.) One must switch the xpath context to the iframe. I tried this in a executeScript:
self.driver.switch_to.frame(frame_reference=self.driver.find_element_by_xpath(xpath="//iframe[@id='sp_message_iframe_533914']"))
This yields an error: Error in executeScript code: self.driver is undefined

2.) One can obmit the context switch by setting the scope of the xpath to the iframe’s contentDocument property:
var iframe = document.getElementById("sp_message_iframe_533914")[0];
var theButton = document.evaluate('/html/body/div/div[2]/div[3]/div[2]/button[@class="sp_choice_type_11"]', iframe.contentDocument,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
Error now: Error in executeScript code: document.getElementById(…) is null

Can somebody help?

UPDATE:

I can address the iframe without the following [0] like this:

var iframe  = document.getElementById("sp_message_iframe_533914");
console.log(iframe)

But it’s property contentDocument is null, so the following does not work (Error: Error in executeScript code: Document.evaluate: Argument 2 is not an object):

var theButton = document.evaluate("/html/body/div/div[2]/div[3]/div[2]/button[@class='sp_choice_type_11']", iframe.contentDocument,null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue

(this iframe is the cookie consent dialog on https://www.finanzen.net)

UPDATE:

I’m really desperate with this cookie button. Now I can tell if it’s there by checking that document.getElementById ("sp_message_iframe_533914") is not null. But I still can’t click it. All my attempts to use xpath with the iframe failed. So I tried xclick with an image. It works when the browser is on my second screen but not on the main notebook screen. Then I tried xclick ocr=Alle akzeptieren@pos=2. That will click anywhere (position the mouse), but not where the search text is.

Does anyone have any idea how to click on the ‘Alles akzeptieren’ button on the page https://www.finanzen.net ?

@newuserkantu @admin @Emil_Szymecki

{
“Name”: “cmp_killer”,
“CreationDate”: “2021-7-30”,
“Commands”: [
{
“Command”: “open”,
“Target”: “https://www.finanzen.net/”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “selectFrame”,
“Target”: “index=1”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “executeScript”,
“Target”: “document.querySelector(‘button[title="Alle akzeptieren"]’).click()”,
“Value”: “”,
“Description”: “”
}
]
}

Work for me

But command selectFrame should have parametr id=xyz in target
index is weak it’s not readable

2 Likes

Thank you, you made my day!

@Emil_Szymecki You are right about selectFrame. I developed using index=1 and now all of a sudden it changed to 2 on that page. But not always :frowning: Instead id= would be helpful @admin