Handling cookies in using Serenity BDD

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]")));

((JavascriptExecutor)driver).executeScript(“arguments[0].click();”, element);

4 Likes

Can you add the URL and descrie a bit what exactly you want to do?

I would say that generic anwers on how to click a button will not really help and maybe a more hands on approach is the road to take.

Just paste the URL and I am sure someone will respond with some code or ideas

2 Likes

Are we talking about https://serenitydojo.teachable.com/ ?
What is the barrier here - other than having an account on their platform?

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.

2 Likes

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?

1 Like

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?

try join at Sign in - Google Accounts

can you please give me 20mins.Il join!

1 Like

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)

$x("//*[contains(text(),‘Akzeptieren’)]")[0].click()

works, but it’s not brilliant, although it illustrates 2 ways to solve this one

  1. 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.
  2. 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 :

driver.execute_script(“arguments[0].click();”, element)

Keen to know how you wrote a good locator for this element.

1 Like

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.

1 Like

I have also tried the below code to perform click action but it did not work :

WebElement webElement = new WebDriverWait(driver, 20).until(ExpectedConditions. elementToBeClickable(By. xpath("//*[contains(text(),‘Akzeptieren’)]")));

Actions action = new Actions(driver);

action.moveToElement(webElement).click().build().perform();

Shadow shadow = new Shadow(driver);
WebElement webElement = shadow.findElementByXPath("//button[@class=‘cookieboxStartAccept cookieBoxStartbutton’]");
shadow.scrollTo(webElement);

	webElement.click();

Tried the above using a dependency,but did not work

1 Like

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:

1 Like

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions. presenceOfElementLocated(By. xpath ("//*[contains(text(),‘Akzeptieren’)]")));

((JavascriptExecutor)driver).executeScript(“arguments[0].click();”, element);

Tried above code-but getting timeout exception

oh rats, I’ll whip open my trick box and try again in a moment.

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

  1. wait for button
  2. click button
  3. 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.)

1 Like