Home > other >  Can't find element belonging to 'Accept All' button
Can't find element belonging to 'Accept All' button

Time:08-17

I recently started learning Selenium and webscraping in Python. I'm trying to find and click the 'Accept All' button on the pop-up when entering the following site: https://www.sherdog.com, using Chrome. I have tried different things and have red what I could find on stackoverflow describing similar problems. To no avail. I always get the NoSuchElementException (or NoAlertPresentException).

I have tried the following things:

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

driver.get('https://www.sherdog.com')
driver.find_element(By.CLASS_NAME, 'Button__StyledButton-a1qza5-0 incZp')
driver.switch_to.alert
try:
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'Button__StyledButton-a1qza5-0 incZp')))
except:
    print("An exception occurred")

I also thought I might have to switch frame using driver.switchTo().frame(driver.findElement(By.id("rufous-sandbox"))), but am honestly unsure which frame to select. When looking through the HTML code (which I just started learning) I see some references to JavaScript (of which I have zero knowledge). Maybe that is causing me trouble?

If anybody could provide some insight, or point me in the right direction, would be greatly appreciated.

CodePudding user response:

To click on the element Accept Cookies you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.sherdog.com")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div>a.cnaccept"))).click()
    
  • Using XPATH:

    driver.get("https://www.sherdog.com")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cnaccept']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

CodePudding user response:

  1. There are 2 objects you need to close there and they are not alerts. So driver.switch_to.alert is not relevant here.
  2. Always try using stable unique locators. Class names like incZp may often be dynamic and not reliable.
  3. Button__StyledButton-a1qza5-0 incZp are actually 2 class names, so you have to use CSS_SELECTOR or XPATH to work with them.
  4. It is always preferred to wait for element visibility, not just existence when you going to click on that element.

This should work:

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

driver.get('https://www.sherdog.com')
wait.until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(),'Continue')]"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#cookieNotice a.cnaccept"))).click()

CodePudding user response:

image

here is a image of what it looks in devtools hope this helps (if u mean the accept all cookies button)

  • Related