0

I'm using Selenium to sign-in to a website and send messages in it. However, the website always shows a cookie popup and I don't know how to press the accept button to proceed on the website.

This is the image of the popup

This is the HTML corresponding to the accept button:

<button class="GdprNotification__LinkButton-nj3w6j-1 hRrekE" data-testid="gdpr-accept-button">Accept</button>

I tried doing this

browser.findElement(By.className("GdprNotification__LinkButton-nj3w6j-1 hRrekE")).click();

But it just gives me this error message

UnhandledPromiseRejectionWarning: NoSuchElementError: Unable to locate element: button.GdprNotification__LinkButton-nj3w6j-1 hRrekE

1 Answers1

0

By.className won't take multiple classnames. Additionally, as the element is a dynamic element you have to use await.


Solution

You can use either of the following Locator Strategies:

  • xpath:

    await driver.findElement(By.xpath("//button[@data-testid='gdpr-accept-button']")
    

More canonically,

  • xpath:

    await driver.findElement(By.xpath("//button[@data-testid='gdpr-accept-button' and text()='Accept']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi, thanks for the suggestion. I'm getting this error: UnhandledPromiseRejectionWarning: NoSuchElementError: Unable to locate element: //button[@data-testid='gdpr-accept-button' and text()='Accept'] It's weird because I put the code after I input the password, yet this error occurs before the password is even sent to the website. Any suggestions? – greenbeans841 Dec 22 '20 at 04:09