I have this code:
from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
os.environ['PATH'] = r'/usr/local/bin/geckodriver'
browser = webdriver.Firefox()
browser.get('https://www.investopedia.com/auth/realms/investopedia/protocol/openid-connect/auth?client_id=finance-simulator&redirect_uri=https://www.investopedia.com/simulator/portfolio&state=84830f95-2e14-4468-87b6-45325beadf2c&response_mode=fragment&response_type=code&scope=openid&nonce=ffc0eff2-93bb-4c25-86f2-f0697898c85b')
browser.set_window_size(1500, 500)
browser.set_window_position(x=0, y=0)
username=browser.find_element(By.ID, "username")
username.send_keys("########")
password = browser.find_element(By.ID, 'password')
password.send_keys('#######')
password.submit()
'''
#First code tried
WebDriverWait(browser, 30).until(
EC.element_to_be_clickable(
(By.CLASS_NAME, "pushly_popover-buttons-dismiss pushly-prompt-buttons-dismiss") ,
)
)
'''
'''
#Second code tried
time.sleep(35)
popup = browser.find_element(By.CLASS_NAME, "pushly_popover-buttons-dismiss pushly-prompt-buttons-dismiss")
'''
#Third code tried
time.sleep(35)
popup = browser.find_element(By.XPATH,"//span[text()='Dismiss']")
popup.click()
here are my errors
#First tried
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:188:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:400:5
element.find/</<@chrome://remote/content/marionette/element.js:292:16
#Second tried
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .pushly_popover-buttons-dismiss pushly-prompt-buttons-dismiss
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:188:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:400:5
element.find/</<@chrome://remote/content/marionette/element.js:292:16
#third tried
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //span[text()='Dismiss']
This is the html of the site html
The error code says that the element does not exists, but I have copy paste it and doubled checked it. I've checked the docs and the code is good. https://www.selenium.dev/documentation/webdriver/elements/finders/ I found this post here but I can't seem to understand what I should do Selenium Python find element by CLASS_NAME returns CSS_SELECTOR not found So how can i select and click the dismiss button?
CodePudding user response:
That notification doesn't seem to appear when using Chrome, however appears when using Firefox. The following code will correctly input user/passwd, click the login button, dismiss cookie button if it appears, then dismiss the pushly popup:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
firefox_options.add_argument("--width=1500")
firefox_options.add_argument("--height=500")
# firefox_options.headless = True
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
url = 'https://www.investopedia.com/auth/realms/investopedia/protocol/openid-connect/auth?client_id=finance-simulator&redirect_uri=https://www.investopedia.com/simulator/portfolio&state=84830f95-2e14-4468-87b6-45325beadf2c&response_mode=fragment&response_type=code&scope=openid&nonce=ffc0eff2-93bb-4c25-86f2-f0697898c85b'
browser.get(url)
username = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.ID, "username")))
passwd = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@id='password']")))
username.send_keys("$$$$$$$$$$$$$$")
passwd.send_keys("###########")
print('filled in the details')
t.sleep(3)
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.ID, "login"))).click()
print('clicked the login button')
try:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-reject-all-handler"))).click()
except Exception as e:
print('no cookie button!')
t.sleep(5)
pushly_stuff = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='pushly_popover-buttons-dismiss pushly-prompt-buttons-dismiss']")))
pushly_stuff.click()
print('pushly stuffs dismissed')
Selenium docs: https://www.selenium.dev/documentation/
Make sure your selenium package is up to date, as well as Firefox, and the corresponding geckodriver.