*** Tasks ***
Get Latest bill from EE
Open Available Browser https://ee.co.uk/
Sleep 3s
Click css=body > div:nth-child(16) > div.mainContent > div > div.pdynamicbutton > a.call
Also I know that it is supposed to be better to use id or name for the locator strategy but it seems that button does not have any of these => is my understanding correct?
Without javascript hacks, you can’t click on elements that are hidden or blocked behind other elements (at least with Selenium) … You should dismiss the cookie dialog first.
The locator of “accept all cookies” is “body > div:nth-child(16) > div.mainContent > div > div.pdynamicbutton > a.call”
But it does not work when I use it
=> Click element css:body > div:nth-child(16) > div.mainContent > div > div.pdynamicbutton > a.call
I have tried click button, click element, click link. Nothing is working and I always get the same error: “Element with locator xxxx not found”
Cookie dialog is inside iframe, so if you are using SeleniumLibrary you have to switch to iframe using Select Frame keyword, click button and than unselect iframe using Unselect Frame keyword.
Such cookie locators tend load slowly and in you code I see hardcoded sleep value. So occasionally you will try to perform action before content is loaded.
Thank you for your guidance.
I have tried to select the iframe but it does not work either. I get the error that the element is not found. Although I have selected the iframe and copy the selector for it
Ok, I get that. Now, eventhough I manage to select the frame, I still get an error saying that the button with locator xxxxx cannot be found.
Could it be due to the fact that there are other iframes inside the main iframe? And I would need to select one of those iframe after the main one?
current code:
Open EE website
Wait Until Element Is Visible xpath=//iframe
Select Frame xpath=//iframe[contains(@id, “pop-frame”)]
Wait And Click Button css= body > div:nth-child(16) > div.mainContent > div > div.pdynamicbutton > a.call
Wait And Click Button css= div.pdynamicbutton > a.call
Does it click the element you want?
I’ve not used the CSS selector method much but I would expect that like xpath the simpler (less strict) your selection criteria the more reliable your script will be.
As a general rule of thumb you want to use the minimum number of selectors or elements you can to identify your object because the page structure is likely to change over the development lifecycle (or even sometimes based on form selection), the more of the html structure you are using to find the element you want to act on the more chance for script failure and the script maintenance you’ll need.
I have done xpath too but it does not work either and I have tried with the shorter selector a.call or div.pdynamicbutton > a.call.
Nothing is working. I am really puzzled with this one
Thank you but that does not work. I think it is because there is no title property for the iframes
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //iframe[contains(@title,"TrustArc Cookie Consent Manager”)] because of the following error:
SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string ‘//iframe[contains(@title,"TrustArc Cookie Consent Manager”)]’ is not a valid XPath expression.
(Session info: chrome=104.0.5112.101)
This could be a page loading issue, try waiting before the click, e.g.
Wait Until Element Is Visible //a[text()="Accept all cookies"]
Click Element //a[text()="Accept all cookies"]
or
Wait Until Element Is Visible css= a.call
Click Element css= a.call
Also be careful with
Wait Until Element Is Visible xpath=//iframe
Select Frame xpath=//iframe[contains(@id, “pop-frame”)]
because if there is another iframe loaded before the “pop-frame” you might get an error with Select Frame if the frame hasn’t loaded yet, I would suggest you do this:
Wait Until Element Is Visible xpath=//iframe[contains(@id, “pop-frame”)]
Select Frame xpath=//iframe[contains(@id, “pop-frame”)]
Ok, I have found the solution. It is a combination of Ajay and Dave answers.
After selecting the frame, I need the robot to wait because the frame appears much faster than the link. Then after sleeping for 2s, I need to click Element or click Link.
So, integrating selector optimisation, the program has become like this (and it does work now!):
Open Available Browser https://ee.co.uk/
Wait Until Element Is Visible xpath=//iframe
Select Frame xpath=//iframe[contains(@id, "pop-frame")]
Wait Until Element Is Visible xpath=//a[text()="Accept all cookies"]
Click Element xpath=//a[text()="Accept all cookies"]
Unselect Frame