0

I want to save element data to an excel file via python. I have the code below, I need some help why the line where

element.click()

gives an error. Even though I put the click() method upper line, but i need it to be in line below.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(r"C:\Users\Admin\Downloads\chromedriver_win32 (1)\chromedriver.exe")
driver.get("https://www.nba.com/schedule?pd=false&region=1")
driver.implicitly_wait(30)
element_to_click=driver.find_element(By.ID,"onetrust-accept-btn-handler").click()
    element_to_click.click() 'error
element_to_save=driver.find_element(By.XPATH,"//div/div/div/div/h4")
#element_to_save.to_excel("3row,3column)")
driver.quit()
1
  • 1
    Your code doesn't work because you assign the actual click() event to variable element_to_click. Then you try to click again on the .. event? of course, you get an error. Anyway, you have a better solution now. Commented Sep 19, 2022 at 19:33

1 Answer 1

1

This is one way to reject/accept cookies on that website:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')

chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)

url = 'https://www.nba.com/schedule?pd=false&region=1'
browser.get(url)
try:
    wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
    print('accepted cookies')
except Exception as e:
    print('no cookie button!')

Setup is selenium/chrome on linux - just observe the imports and the part after defining the browser/driver. Selenium documentation can be found at https://www.selenium.dev/documentation/

Sign up to request clarification or add additional context in comments.

9 Comments

It does not close pop-window. I need to click 'Accept' button
@xlmaster I updated the code to accept the cookies now - it was a matter of changing the ID value.
Perfect!! works great! I liked your way.
@xlmaster Does this resolved your question? If so, why don't you accept the answer?
Believe me, I have similar thoughts... But you know, sometimes we can upvote people giving good answers here ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.