Can someone please help me in understanding how to handle or accept cookies layer button after the launch of my application URL in Serenity Dojo Web automation that uses Selenium.
I tried the below but it does not work:
1.driver.findElement(By.xpath("/html/body/div[5]//div/div/div/div/div[4]/button[1]")).click();
2.WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions. elementToBeClickable (By. xpath ("/html/body/div[5]//div/div/div/div/div[4]/button[1]")));
I was hepeing to be able to find the accessibility ID here, accessing the DOM using expaths like that are a recipe for this not working and making you feel that selenium is the wrong tool to be using, it really is not, itās just that the training is leading you to the wrong places.
https://ratioform.de is the URL
I need to accept the cookies after the app url is launched.
The element - button, i need to click is attached to a #shadow-root
How to handle such an element?
that looks like a CSS ID, but also might be in the shadow DOM, so you will want to learn about shadow DOM a bit too, since itās an alternative traversal mechanism or āmapā, let me go see if i can reach it and double check for you. Do you have a Google account? we can do an interactive session if you like and I can actually show you?
Sorry my internet dropped out there, still puzzled why the classname was not a valid locator in this case (even though I was using xpath syntax to fetch by classname which is backwards)
works, but itās not brilliant, although it illustrates 2 ways to solve this one
the element is āinvisibleā for starters. seleniumās find-element-by function will time out on this element. this happens because Selenium is always making a best guess at whether an element is possibly obscured.
the element might not be āclickableā either as far selenium is concerned, hence then one needs to invoke some javascript in the browser too. How that works will depend on your test framework
for example I have this code line called often in my test framework
#scroll to visible
self.driver.execute_script(āarguments[0].scrollIntoView();ā, element)
and thus I use this to click on any āunclickableā or elements selenium thinks are invisible this will help there :
Oh, and @muchapathi
Welcome to MOT, sorry to drag you into a meeting with strangers, and then have my internet die. You did deal with it well though. Hope to learn from ResterTest about the partial ID matching locators as well. I have been moving on a learn-as-you-go myself, still more experiences to be had.
If you canāt find the answer, John Ferguson has a LinkedIn group (Agile Test Automation Secrets) where you can maybe get help, heās pretty active there from what Iāve noticed:
Okay, Swetha, the problem seems to be that there is an animation, the webpage is changing all the while and that element even though itās the same element before and after waiting, if you are too quick, no dice. It looks like there are 2 ways
Option #1
Basically there are 2 options open to us , wait for the page to stop āloadingā
or keep trying to dismiss the button in a loop until the button disappears. Here is the python code that waits for the page to stop animating, then clicks the button using a javascript execute
chrome_wd = webdriver.Chrome(ChromeDriverManager().install())
chrome_wd.get(āhttps://ratioform.de/ā)
#
wait_page_load(chrome_wd)
element = WebDriverWait(chrome_wd, 20).until(EC.presence_of_element_located(
(By.XPATH, ā//[contains(text(),āAkzeptierenā)]")))
chrome_wd.execute_script(āarguments[0].click();console.log(āclicked it!!!ā);ā, element)
# verify dismissed
try:
element = WebDriverWait(chrome_wd, 2).until(EC.presence_of_element_located(
(By.XPATH, "//[contains(text(),āAkzeptierenā)]ā)))
except TimeoutException as e:
pass
print(āCookies box is goneā)
time.sleep(10)
chrome_wd.quit()
Option #2
personally I would prefer this approach
wait for button
click button
if button still exists goto step 1
with a timeout to prevent hangs of course. That algorithm would feel cleaner to me because itās very explicit what we are up to.
Finally
I am normally raising a bug with the developers to fix any places where any web element that needs automating are not easy to get to (the animation problem is a marketing problem which is a battle Iām not prepared to fight today.) has a unique ID. Iām trying lately to rise these bugs as blockers to force the developers to pro-actively make the app testable before they hand it to QA. It saves a load of time.
(I may be wrong, as I mentioned, not been doing web hacking long, many folk been doing this with a year for every month of my experience.)