Trouble finding element

Question:

I am very new to coding and I am trying to make a form filler on Nike.com using the Selenium Chrome webdriver. However, a pop-up comes up about cookied and I am finding it hard to remove it so I can fill out the form.
This is what it looks like
and my code looks like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

#Initialise a chrome browser and return it
def initialisebrowser():
    browser=webdriver.Chrome(r'''C:Usersben_sDownloadschromedriver''')
    return browser

#Pass in the browser and url, and go to the url with the browser
def geturl(browser, url):
    browser.get(url)

#Initialise the browser (and store the returned browser)
browser = initialisebrowser()

#Go to a url(nike.com) with the browser
geturl(browser,"https://www.nike.com/gb/en_gb/s/register")
button = browser.find_element_by_class_name("nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button")
button.click()

When I run this code, I get this error:

Traceback (most recent call last):
  File "C:Usersben_sDesktopNike Account Generator.py", line 19, in <module>
    button = browser.find_element_by_class_name("nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button")
  File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 557, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 957, in find_element
    'value': value})['value']
  File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "C:PythonPython36libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button"}
  (Session info: chrome=67.0.3396.87)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)

Any ideas, pointers or solutions to the problem are much apprieciated

Asked By: Ben S.

||

Answers:

use time.sleep to wait until the popup will load, and after that you can use cookie-settings-button-container class that is parrent of the btn this will work.

time.sleep(1)                                                                                                                                                                   
button = browser.find_element_by_class_name("cookie-settings-button-container")
button.click()
Answered By: Druta Ruslan

find_element_by_class_name recives one class as parameter

browser.find_element_by_class_name('yes-button')

The parameter you provided is combination of all the webelement classes, and is used by css_selector

browser.find_element_by_css_selector('.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')

Note that you need to add . before the first class as well, otherwise it is treated as tag name. For example in this case

browser.find_element_by_css_selector('button.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')

To make sure the button exists before clicking on it you can use explicit wait

button = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CLASS_NAME, 'yes-button')))
button.click()
Answered By: Guy

I had a similar problem once. And I really didn’t need the cookie pop-up for my test. So I thought of removing the cookie element from the page before I do what I needed to do. And I accomplished it by using the JavaScriptExecutor.

You can check this article here –> stackoverflowlink

And then inject the JavaScript code into your selenium script.

Answered By: GabrielFethi