How to locate xpath for Pop-up

Hello, can someone please help me please

I have tried all the possible xpath of this pop up but still it did not work.

// Clicking on Not Now to turn off Notification pop-up button
driver.findElement(By.xpath(“//button[text()='aOOlW HoLwm ']”)).click();
driver.findElement(By.xpath(“//button[contains(text(), 'aOOlW HoLwm ')”)).click();
driver.findElement(By.xpath(“//button[@class=‘aOOlW___HoLwm_’]”)).click();
driver.findElement(By.xpath(“//[@button=‘aOOlW___HoLwm_’]“)).click();
driver.findElement(By.xpath(”//div[@class=‘mt3GC’]/button[2]“)).click();
driver.findElement(By.xpath(”//
[@button='aOOlW HoLwm ']”)).click();
driver.findElement(By.xpath(“//div[contains(@class,‘Not Now’)]”)).click();
driver.findElement(By.xpath(“//button[contains(text(),‘Not Now’)”)).click();

Can someone help what is wrong here?

Can you be more specific on what isn’t working? Is it not finding the element? Is it finding the element, but not being able to click on it?

Hi Brandon_Hein, sorry about that. I’m trying to close the Pop up notification and click on the ‘Not Now’ button. I’ve tried to locate the xPath of ‘Not Now’ and click on it so that it will close, but it didn’t work. I’m getting error “Unable to locate element”. Not sure if my xPath is wrong. Please help. :slight_smile:

Based on the limited HTML you’ve made available, the following CSS selectors will target the Not Now button:

button.aOOlW.HoLwm

More robust:

div.piCib button.aOOlW.HoLwm

This Groovy/JavaScript combination will click the button:

String js = "document.querySelector('button.aOOlW.HoLwm').click();"
WebUI.executeJavaScript(js, null)

If the HTML you posted is inside an iframe, more work will be needed.

If you want to stick with XPath, wait for Brandon.

It looks to me like your class attributes may be generated dynamically. Try locating by the actual button text instead:

//div[@role=“dialog”]//button[text()=“Not Now”]

Still did not work. Maybe I need to use other website for practice lol

One other thing to try, it may be that the element isn’t present right away, you may need to wait for it:

new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@role='dialog']//button[text()='Not Now']")))
1 Like

It works @Brandon_Hein

new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@role=‘dialog’]//button[text()=‘Not Now’]"))).click();

but kinda not happy since I dont understand the above syntax waaah

Which part, the WebDriverWait usage? Or the xpath?

This part —> //div[@role=‘dialog’]
Do I need to use this for every pop up notification?

Also, the WebDriverWait usage. Why do we have to include that? 5 means 5 seconds?

Do you need to? Probably not. Would it be a good idea? Probably. It basically narrows down your search to only elements that live within that dialog. If you left it off, and just did:

//button[text()=“Not Now”]

it would probably still work fine, but it’s less safe. What if, for whatever reason, there was another button with that text somewhere else on the page? Putting the parent element just makes it a more precise locator.

Right, the 5 is a timeout argument. Lets say that that element NEVER appeared. Would you want the code to sit there and wait forever? Probably not. So we give it a timeout, which basically says “if the condition isn’t met within x seconds, move on”.

Whenever you have questions about particular API usage, it’s a good practice to learn to read Javadoc/API docs. You can review how to use WebDriverWait here: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html

I see. Thank you very much @Brandon_Hein. I am just new to Automation and I think I need to learn a lot :slight_smile:

Hey, I’m not new to automation, and I still need to learn a lot too :smiley: I’ll mark a solution to this post.

1 Like

Sure! Thank you again :slight_smile: