Jonathan Sainsbury
Jonathan Sainsbury

Reputation: 43

How can I interact with a cookies modal using Selenium?

I am looking to automate some clicks on this page, which I am using as a test as I am just learning Selenium.

https://www.pgatour.com/competition/2019/safeway-open/leaderboard.html

I am unable to get my code to automatically accept and close the cookies overlay when I load the page, which is preventing me from getting any further.

I have tried various ways to identify the element, but have received the same message every time.

I believe this is the HTML for the element I need to identify:

<a class="call" tabindex="0" role="button">Agree and Proceed</a>

The latest version of my code is this:

browser = webdriver.Chrome() 
url = "https://www.pgatour.com/competition/2019/safeway-open/leaderboard.html"
browser.get(url) 
cookieaccept = browser.find_element_by_xpath("//a[@class = 'call']")
cookieaccept.click()

The error message is:

selenium.common.exceptions.NoSuchElementException:
  Message: no such element: Unable to locate element:
  {"method":"xpath","selector":"//a[@class = 'call']"}

Upvotes: 4

Views: 5480

Answers (3)

KunduK
KunduK

Reputation: 33384

There is an iframe which title TrustArc Cookie Consent Manager . You need to switch to iframe first to access the element.

Induce WebDriverWait and frame_to_be_available_and_switch_to_it Induce WebDriverWait and element_to_be_clickable

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
url = "https://www.pgatour.com/competition/2019/safeway-open/leaderboard.html"
browser.get(url)
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@title="TrustArc Cookie Consent Manager"]')))
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='call'][text()='Agree and Proceed']"))).click()

Browser Snapshot: enter image description here

To close the button add this code after that.

WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH,"//a[@id='gwt-debug-close_id'][@class='close']"))).click()

Upvotes: 6

0xM4x
0xM4x

Reputation: 470

In the URL that you want to test, the cookies located in an iframe. in order to click on sth in this iframe, first you have to find the iframe. then you have to switch to the iframe. after that you can come back to the default page.

frame_reference = driver.find_element_by_class_name("gwt-Frame")
driver.switch_to.frame(frame_reference)

then find your element (agree and proceed) and click on it.

then:

driver.switch_to.default_content()

Upvotes: 1

Zakaria Shahed
Zakaria Shahed

Reputation: 2697

The easiest way to hide cookie banner which is shown for gdrp compliance. When you accept cookie modal its write a cookie on the browser for your system.

Like enter image description here

You can do same thing manally. After open browser write this cookie and reload your browser.Then cookie modal will not open in feature.

self.webdriver.add_cookie({'name' : 'notice_gdpr_prefs', 'value' : '0,1,2:', 'domain' : self.store['base'] + url})
        self.webdriver.add_cookie({'name' : 'notice_preferences', 'value' : '2:', 'domain' : self.store['base'] + url})

I don't have much idea about python cookie write.You can follow this tutorial to write cookie

https://chercher.tech/python/python-selenium-cookies

Python create cookies and then load a page with the cookies

Upvotes: 3

Related Questions